0

The loadInventory function with a string filename as parameter reads the contents from inventory.txt

def loadInventory(filename):
    inventory = {}
    inventoryFile = open(filename)
    for line in inventoryFile:
        itemID,itemStock = line.split(":")
        inventory[itemID] = itemStock
        inventory = {itemID: itemStock for itemID, itemStock in inventory.items()}
        inventory[itemID] = [itemStock.replace('\n', '')]
    
    # print(f"{inventory}")
    return inventory

def main():
    print(loadInventory('Inventory.txt'))
main()

Inventory.txt:

C05:10,10,5,4
C01:0,20,10,5
C11:10,20,10,1
C03:0,0,10,0
C10:1,1,1,1

Output:

{'C05': ['10,10,5,4'], 'C01': ['0,20,10,5'], 'C11': ['10,20,10,1'], 'C03': ['0,0,10,0'], 'C10': ['1,1,1,1']}

Intended Output:

 {'C05':[10,10,5,4], 'C01':[0,20,10,5], 'C11':[10,20,10,1],
      'C03':[0,0,10,0]}
0

1 Answer 1

1

Try replacing this line:

        inventory[itemID] = [itemStock.replace('\n', '')]

To:

        inventory[itemID] = [int(i) for i in itemStock.replace('\n', '').split(',')]

Or:

        inventory[itemID] = list(map(int, itemStock.replace('\n', '').split(',')]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.