Python adding user input to dictionary/list

Question:

I am VERY new to python and have found myself very confused. Can anyone help me out or maybe better explain how I could get the output that I am wanting? I have a current dictionary that includes my "inventory" and list that includes my "dragonLoot". I am trying to create a function that asks the user if they would like to add anything else to their inventory and then add those to my standing dictionary list. I know I have the first function right, but I’m having trouble understanding what to change in the second function. This is what I have so far:

Thank you for any help you can provide,again I am extremely new to this and still struggling with functions

def displayInventory(myInv):
    print("Inventory:")
    total = 0

    for k,v in myInv.items() :
        print(str(v) + " " + k)
        total = total + v
    print("Total number of items:" + str(total)) 

def addToInventory(inventory,addedItems) :
    addedItems = input("What would you like to add to your your Dragon Loot? Enter D when finished ")
    if addedItems not in dragonLoot :
        inventory.update({addedItems})
    if input == "D" :
        return dragonLoot

addToInventory(inventory,addedItems)

inventory = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}#Create your inventory dictonary
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']

displayInventory(inventory) 
Asked By: Emily McWilliams

||

Answers:

Firstly, When you are trying to add a new item to the dictionary, you have to use split() function. It will allow the user to input two values on the same line. I have done some basic changes, do the rest of your own.

def displayInventory(myInv):
    print("Inventory:")
    total = 0

    for k,v in myInv.items() :
        print(str(v) + " " + k)
        total += int(v)
    print("Total number of items:" + str(total)) 

def addToInventory(inventory,dragonLoot) :

    new_Item = int(input("How many Items you want to add? "))
    counter = 0
    while (new_Item != counter):
        
        name_Item, quantity_Item = input("What would you like to add to your your Dragon Loot? Enter D when finished :").split()
        if name_Item not in dragonLoot :
            inventory[name_Item] = quantity_Item
        counter +=1




inventory = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}#Create your inventory dictonary
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
addToInventory(inventory,dragonLoot)
displayInventory(inventory) 
Answered By: Muntasir Aonik
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.