assigning values to a list and change with user input

Viewed 59

I have a string of product parts = 'ABCDEF'. Each letter suppose to have a starting value of 100. The user is supposed to add numbers to the specific letter of their choosing and the value of that letter will be updated accordingly. I'm not supposed to use dictionary for this code. I wrote what I roughly know except those with [?]

def updateInventory():
    startLevel = 100
    partIds = 'ABCDE'
    stockLevel = [startLevel] * len(partIds)
    productCodes = []
    while True:
        partIdentifier = input("Enter part identifier or <ENTER> to end: ").upper()
        if partIdentifier == '':
            break
        # partIdentifier must be in partIds
        if partIdentifier not in partIds:
            print("The part identifier is invalid")
        else:
            #print the part and its current quantity
            print(f"Current stock level for {partIdentifier} = [?]")
            while True:
                # user tp input how much quantity to add
                quantity = int(input("Enter quantity to add: "))
                if quantity < 0:
                    print("The quantity is invalid")
                else:
                    partIds[partIdentifier] += quantity
                    print(f"Updated stock level for {partIdentifier} = [?]")
                    break
updateInventory() 

Enter part identifier or ENTER to end: T

The part identifier is invalid

Enter part identifier or ENTER to end: E

Current stock level for E = 100

Enter quantity to add: 0

The quantity is invalid

Enter part identifier or ENTER to end: E

Current stock level for E = 100

Enter quantity to add: 25

Updated stock level for E = 125

1 Answers

You can make use of writing helper functions to get the index of stockLevel to be updated for the particular partId.

what does index = partIds.index(partIdentifier) do

In one line it gets the access location of any matching input character which is corresponding to StockLevel_list.

partIdentifier is the input char (ex. E), partIds is string ABCDE, index will try to get the character location of given letter in the string ABCDE, then that index will be used to access and update the list StockLevel. since stocklevel is created as a reference to partIds so the index will be identical.

def updateStock(partIds, stockLevel, ipartIdentifier, quantity):
    index = partIds.index(ipartIdentifier)
    stockLevel[index] += quantity
    return stockLevel


def getStockval(partIds, stockLevel, ipartIdentifier):
    index = partIds.index(ipartIdentifier)
    return stockLevel[index]


def updateInventory():
    startLevel = 100
    partIds = 'ABCDE'
    stockLevel = [startLevel] * len(partIds)
    productCodes = []
    while True:
        partIdentifier = input(
            "Enter part identifier or <ENTER> to end: ").upper()
        if partIdentifier == '':
            break
        # partIdentifier must be in partIds
        if partIdentifier not in partIds:
            print("The part identifier is invalid")
        else:
            # print the part and its current quantity
            print("Current stock level for {0} = {1}".format(partIdentifier,
                                                              getStockval(partIds, stockLevel, partIdentifier)))
            while True:
                # user tp input how much quantity to add
                quantity = int(input("Enter quantity to add: "))
                if quantity < 0:
                    print("The quantity is invalid")
                else:
                    stockLevel = updateStock(
                        partIds, stockLevel, partIdentifier, quantity)
                    print("Updated stock level for {0} {1}".format(partIdentifier,
                        getStockval(partIds, stockLevel, partIdentifier)))
                    break


updateInventory()

Output:

Enter part identifier or <ENTER> to end: a
Current stock level for A = 100
Enter quantity to add: 50
Updated stock level for A = 150
Enter part identifier or <ENTER> to end: e
Current stock level for E = 100
Enter quantity to add: 25
Updated stock level for E = 125
Enter part identifier or <ENTER> to end: a
Current stock level for A = 150
Enter quantity to add: 12
Updated stock level for A = 162
Enter part identifier or <ENTER> to end:
Related