Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List logic
#8
(Apr-22-2017, 05:44 PM)ichabod801 Wrote:
for item in dragonLoot:
    stuff[item] = stuff.get(item, 0) + 1
This works better with a defaultdict, then you can just add one to the key directly, without having to do the get with a default value of zero.

tried it and this happens:
stuff = {'rope' : 1, 'torch' : 6, 'gold coin' : 42, 'dagger' : 1, 'arrow' : 12}

def displayInventory(inventory):
    print('Inventory:')
    item_total = 0
    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        item_total += v
    print('Total number of items: ' + str(item_total))

def addToInventory(inventory, addedItems):
    for item in addedItems:
        inventory[item] = inventory.get(item, 0) + 1


dragonLoot = ['gold coin', 'dagger', 'gold coin', 'ruby']
stuff = addToInventory(stuff, dragonLoot)
displayInventory(stuff)
Error:
/usr/bin/python3.5 /root/PythonProjects/AutomateTheBoringStuff/inventory.py Inventory: Traceback (most recent call last):   File "/root/PythonProjects/AutomateTheBoringStuff/inventory.py", line 18, in <module>     displayInventory(stuff)   File "/root/PythonProjects/AutomateTheBoringStuff/inventory.py", line 6, in displayInventory     for k, v in inventory.items(): AttributeError: 'NoneType' object has no attribute 'items' Process finished with exit code 1
Something like this is what I was thinking???


def addToInventory(inventory, addedItems):
    for item in addedItems:

        if item in inventory.keys():
            inventory[item] += 1
            print(inventory)
            return inventory

        elif item not in inventory.keys():
            inventory[item] = 1
            print(inventory)
            return inventory




dragonLoot = ['gold coin', 'dagger', 'gold coin', 'ruby']
stuff = addToInventory(stuff, dragonLoot)
Huh

When i add return inventory to the function it doesn't add all the inventory in dragonLoot:

stuff = {'rope' : 1, 'torch' : 6, 'gold coin' : 42, 'dagger' : 1, 'arrow' : 12}

def displayInventory(inventory):
    print('Inventory:')
    item_total = 0
    for k, v in inventory.items():
        print(str(v) + ' ' + k)
        item_total += v
    print('Total number of items: ' + str(item_total))

def addToInventory(inventory, addedItems):
    for item in addedItems:
        inventory[item] = inventory.get(item, 0) + 1
        return inventory


dragonLoot = ['gold coin', 'dagger', 'gold coin', 'ruby']
stuff = addToInventory(stuff, dragonLoot)
displayInventory(stuff)
Output:
/usr/bin/python3.5 /root/PythonProjects/AutomateTheBoringStuff/inventory.py Inventory: 1 rope 43 gold coin 12 arrow 6 torch 1 dagger Total number of items: 63 Process finished with exit code 0
Reply


Messages In This Thread
List logic - by Low_Ki_ - Apr-22-2017, 02:03 AM
RE: List logic - by Low_Ki_ - Apr-22-2017, 03:08 AM
RE: List logic - by ichabod801 - Apr-22-2017, 10:50 AM
RE: List logic - by Low_Ki_ - Apr-22-2017, 04:56 PM
RE: List logic - by idontreallywolf - Apr-22-2017, 11:10 AM
RE: List logic - by tomhath - Apr-22-2017, 03:41 PM
RE: List logic - by ichabod801 - Apr-22-2017, 05:44 PM
RE: List logic - by Low_Ki_ - Apr-22-2017, 06:53 PM
RE: List logic - by ichabod801 - Apr-22-2017, 09:24 PM
RE: List logic - by Low_Ki_ - Apr-22-2017, 11:46 PM
RE: List logic - by Low_Ki_ - Apr-23-2017, 02:29 AM
RE: List logic - by wavic - Apr-23-2017, 04:21 AM
RE: List logic - by Low_Ki_ - Apr-23-2017, 04:31 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020