AutomateTheBoringStuff.Ch05.Projects package

Submodules

AutomateTheBoringStuff.Ch05.Projects.P1_gameInventory module

Fantasy game inventory

This program models a player’s inventory from a fantasy game.

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
AutomateTheBoringStuff.Ch05.Projects.P1_gameInventory.stuff

Dictionary with item names as keys and their counts as values.

Type:dict
AutomateTheBoringStuff.Ch05.Projects.P1_gameInventory.displayInventory(inventory: dict) → None[source]

Display inventory

Displays each key in a given inventory dictionary.

Parameters:inventory – Inventory dictionary to display.
Returns:None. Prints out inventory.
AutomateTheBoringStuff.Ch05.Projects.P1_gameInventory.main()[source]

AutomateTheBoringStuff.Ch05.Projects.P2_gameInventory module

Fantasy game inventory 2.0

This program models a player’s inventory from a fantasy game with the ability to add to the 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

The addedItems list can contain multiples of the same item.

AutomateTheBoringStuff.Ch05.Projects.P2_gameInventory.addToInventory(inventory: dict, addedItems: list) → dict[source]

Add to inventory

Adds given list of items to given dictionary inventory.

Parameters:
  • inventory – Dictionary inventory to add items to.
  • addedItems – List of strings to add to inventory.

Note

If the item already exists in the dictionary, its count is incremented.

AutomateTheBoringStuff.Ch05.Projects.P2_gameInventory.main()[source]

Module contents