Git Product home page Git Product logo

cc-game-inventory's Introduction

Game inventory

Story

You just started to work at a game developer company named Eastwood Studios and they decided to create a new text-based role-playing video game. You are responsible for implementing the inventory system of the game which will store, show and manage the acquired goods of the player.

As there are other developers on this project implementing other parts of the game it's crucial to use the provided function names and conform with the requirements to have a well-working final product. Also note that your git commit messages communicate you intents to your team-mates as well, so spend time on writing meaningful ones!

What are you going to learn?

You will learn and practice how to do the following things with Python:

  • use loops
  • use the dictionary data structure
  • printing data on console in an organized way
  • work with files
  • handle errors
  • write nice git commit messages

Tasks

  1. Write a function named display_inventory(inventory) that would take any possible inventory as dictionary and display its contents.

    • Calling the function with a non-empty dictionary argument results in displaying each key, followed by a colon, then a space, then the corresponding value, then a newline
    • Calling the function with an empty dictionary results in displaying nothing
  2. Write a function named add_to_inventory(inventory, added_items) that would add the contents of the added_items list to the inventory dictionary.

    • Calling the function with a dictionary and items which aren't in the inventory yet results in that the items are added to the inventory as keys and values are set to 1
    • Calling the function with a dictionary and items which are already in the inventory results in that those items' value are incremented by 1
    • The function could handle if the added_items argument contains multiple occurences of the same item, then dictionary values are incremented by the number of occurences
  3. Write a function named remove_from_inventory(inventory, removed_items) that would remove the contents of the removed_items list from the inventory dictionary.

    • Calling the function with a dictionary and items which are already in the inventory results in that the values of the keys matching the items are decreased by 1
    • If any value in the dictionary becomes 0 or less after decreasing, the corresponding key is removed
    • The function could handle if the removed_items argument contains multiple occurences of the same item, then dictionary values are decreased by the number of occurences
    • Calling the function with a dictionary and items which aren't in the inventory results in no change in the dictionary
  4. Write a function named print_table(inventory, order) that takes an inventory as parameter and displays it in an ordered, well-organized table with each column right-aligned.

    • Calling the function with a non-empty dictionary argument results in displaying a two-column table with all the items, where the first column contains the keys and the second column contains the values as seen in the example below:

      -----------------
      item name | count
      -----------------
      gold coin |    45
          arrow |    12
          torch |     6
         dagger |     2
           rope |     1
           ruby |     1
      -----------------
      
    • The printing order can be set with the order parameter as follows:

      • empty (by default) means the table is unordered
      • count,desc means the table is ordered by count (of items in the inventory) in descending order
      • count,asc means the table is ordered by count in ascending order
  5. Write a function named import_inventory(inventory, filename) which can import new inventory items from a CSV file.

    • The function can handle CSV files containing items in the following format: ruby,rope,ruby,gold coin,ruby,axe
    • Calling the function with a dictionary and items in a file in comma separated format (e.g. rope, torch, arrow) which aren't in the inventory yet results in that the items are added to the inventory as keys and values are set to 1
    • Calling the function with a dictionary and items in a CSV file which are already in the inventory results in that those items' value are incremented by 1
    • The function could handle if the CSV file contains multiple occurences of the same item, then dictionary values are incremented by the number of occurences
    • If not specified, the filename argument is by default import_inventory.csv
    • If the file provided in the filename argument cannot be reached on the disk, then the error message File '<filename>' not found! is shown on the console output
  6. Write a function named export_inventory(inventory, filename) which can export all inventory items to a CSV file.

    • Calling the function with a non-empty dictionary and a filename argument results in the dictionary keys to be saved in CSV format in the file
    • If there are keys in the dictionary with values greater than 1, then the key is saved into the file as many times as the value
    • If not specified, the filename argument is by default export_inventory.csv
    • The file denoted in the filename argument is automatically created if not exists, and is overwritten if already exists
    • If the user executing the function does not have write access in the folder where the script is executed then the error message You don't have permission creating file '<filename>'! is shown on the console output
  7. Commit your changes to git

    • Executing git log in the project directory shows at least as many commits as many tasks were solved
    • Executing git log in the project directory shows commit messages which are easy to understand, and describe the changes they introduced

General requirements

None

Hints

  • You can use a for loop to loop through all the keys in a dictionary.
  • You can use F-strings to print out variables mixed with text comfortably.
  • Feel free to include any code in the main part of the program (outside of the functions) during development if you want to try out your functions. Make sure you remove these lines or protect them with an if __name__ == "__main__": code snippet before pushing your code.
  • Check all the materials - we included a must read post about git commit messages
  • This project has public tests that can check whether your functions are working as intended. Run the test file with python3 game_inventory_test.py -v to get a detailed output. Note that passing all tests not necessarily means passing all requirements!

Starting your project

Background materials

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.