Feedback requested for practice project - see bottom of this page https://automatetheboringstuff.com/chapter5/


#1

Hi,

I have finished the Python 2 course on here, now I am going through automatetheboringstuffwithpython.com and just finished an assignment. Since I am still a beginner, I’m wondering if people with more experiecen on here could provide some feedback on how to do it better. Below I have copied the assignment, my code and it’s result. Thanks!

The assignment is:

Fantasy Game Inventory

You are creating a fantasy video game. The data structure to model the player’s inventory will be a dictionary where the keys are string values describing the item in the inventory and the value is an integer value detailing how many of that item the player has. For example, the dictionary value {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} means the player has 1 rope, 6 torches, 42 gold coins, and so on.

Write a function named displayInventory() that would take any possible “inventory” and display it like the following:

Inventory: 12 arrow 42 gold coin 1 rope 6 torch 1 dagger Total number of items: 62

Hint: You can use a for loop to loop through all the keys in a dictionary.

inventory.py 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(): # FILL IN THE CODE HERE print("Total number of items: " + str(item_total)) displayInventory(stuff)

List to Dictionary Function for Fantasy Game Inventory

Imagine that a vanquished dragon’s loot is represented as a list of strings like this:

dragonLoot = [‘gold coin’, ‘dagger’, ‘gold coin’, ‘gold coin’, ‘ruby’]

Write a function named addToInventory(inventory, addedItems) , where the inventory parameter is a dictionary representing the player’s inventory (like in the previous project) and the addedItems parameter is a list like dragonLoot . The addToInventory() function should return a dictionary that represents the updated inventory. Note that the addedItems list can contain multiples of the same item. Your code could look something like this:

def addToInventory(inventory, addedItems): # your code goes here inv = {‘gold coin’: 42, ‘rope’: 1} dragonLoot = [‘gold coin’, ‘dagger’, ‘gold coin’, ‘gold coin’, ‘ruby’] inv = addToInventory(inv, dragonLoot) displayInventory(inv)

The previous program (with your displayInventory() function from the previous project) would output the following:

Inventory: 45 gold coin 1 rope 1 ruby 1 dagger Total number of items: 48

Below, my code that got the results but probably could be way more efficient and/or pretty



#to be added loot, see add_to_inventory function (given as list in the assignment)
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']

#starting inventory (given as dictionary in the assignment)
inventory = {
  'rope': 1,
  'torch': 6,
  'gold coin': 42,
  'dagger': 1,
  'arrow': 12
}

#function to print inventory, including total items no.
def display_inventory(lst):
  total_items = 0
  for i in lst:
    print(str(lst[i]) + " " + str(i))
    total_items += lst[i]
  print ("Total number of items: " + (str(total_items)))
display_inventory(inventory)

#function to add new item set to inventory: The first part is to change the list of loot to a dictionary, the second is to either add the amount to the items already in inventory, or to add the key value pair altogether. The last part isto print current inventory just like the function before 

#to get an empty space in the output
print

def add_to_inventory(inv, added_items):
  added_itemsQ = {}
  total_items = 0
  for item in added_items:
    added_itemsQ[item] = 1
  for i in added_itemsQ:
    if not i in inv: 
      inv[i] = added_itemsQ[i]
    else: 
      inv[i] = inv[i] + added_itemsQ[i]
  for i in inv:
    print(str(inv[i]) + " " + str(i))
    total_items += inv[i]
  print ("Total number of items: " + (str(total_items)))
  
add_to_inventory(inventory, dragonLoot)



Result was:
1 rope
42 gold coin
6 torch
1 dagger
12 arrow
Total number of items: 62

43 gold coin
2 dagger
6 torch
1 rope
12 arrow
1 ruby
Total number of items: 65


#2

In display_inventory, try using an if-else in the loop:

for i in lst:
    if lst[i] == 1:
        print("1 " + str(i))
    else:
        print(str(lst[i]) + " " + str(i) + "s")
    total_items += lst[i]

#3

Thanks man, looks better that way!