Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 2.97 KB

File metadata and controls

49 lines (36 loc) · 2.97 KB

Hints

General

1. Create an inventory based on a list

  • You need a for loop to iterate the list of items, then insert each item in the dictionary if missing and increment the item count using the dictionary accessor.
  • You can use dict.setdefault to make sure the value is set before incrementing the count of the item.
  • This function should return a dict.

2. Add items from a list to an existing dictionary

  • You need a for loop to iterate the list of items, then insert each item if not already in the dictionary and increment the item count using the dictionary accessor.
  • You can use dict.setdefault to make sure the value is set before incrementing the count of the item.
  • The function add_items can be used by the create_inventory function with an empty dictionary in parameter.
  • This function should return a dict.

3. Decrement items from the inventory

  • You need for loop to iterate the list of items, if the number of items is not 0 then decrement the current number of items.
  • You can use the check key in dict that returns True if the key exists to make sure the value is in the dictionary before decrementing the number of items.
  • This function should return a dict.

4. Remove an item entirely from the inventory

  • If item is in the dictionary, remove it.
  • If item is not in the dictionary, do nothing.
  • This function should return a dict.

5. Return the inventory content

  • You need to use a for loop on the inventory and if the number of item is greater of 0 then append the tuple to a list.
  • You can use dict.items() to iterate on both the item and the value at the same time, items() returns a tuple that you can use or deconstruct, if needed.
  • This function should return a list of tuples.