[nfbcs] Can anyone help me understand what I'm doing wrong here?

Bryan Duarte bjduarte at asu.edu
Wed Jul 25 03:26:52 UTC 2018


Hello Lanie,

There are a couple things I will mention here. In software there are always multiple methods for solving a problem so even though I might share with you one way there are many others. 

First of all when you set up your for loop to iterate through the dictionary you have a k and v. Only the v is necessary. Python allows you to loop through data structures such as lists dictionaries, tuples, etc simply by using the syntax
for i in dictionary:

Next you are using the items() method which might work but typically when dealing with dictionaries you use the .get() method to get the value.
item_value = dictionary.get(key)

Finally you will want to sum up the values of the dictionary which you get from the get method. 

I will post one method for doing this for you to review and hopefully be able to solve the second part on your own;) 

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

def displayInventory(inventory):
  print("Inventory:")
  item_total = 0

  for item_name in inventory:
    item_value = inventory.get(item_name)
    item_total += item_value

  print("Total number of items: " + str(item_total))

displayInventory(stuff)

Bryan Duarte | software engineer

ASU Computer Science Ph.D Student
IGERT Fellow
Alliance for Person-centered Accessible Technology (APAcT)
Center for Cognitive Ubiquitous Computing (CUbiC Lab)
National Federation of the Blind of Arizona | Affiliate Board Member
Arizona Association of Blind Students | President
Phone: 480-652-3045




More information about the NFBCS mailing list