Python Project: Store

Project Description

You will start to sell items from your awesome store.

You count items sold, i.e. if you sell the three following items.

  1. apple
  2. pear
  3. orange

To keep track of how many items you sell, you can use a dictionary.

The dictionary can use the items as keys, and the number of sold items as the value.

sold_items = {'apple': 0}

Create the following.

  • Use a dictionary to keep track of sold items.
  • Create a user prompt to sell items.
  • Create a status view of items sold.

Step 1 The most important decision

When you need to solve a programming problem, the biggest decision you need to make is how to represent the data, the model, or whatever is the main thing in the project.

This is often not given in the project description.

In the above description, it is, but in most cases, it is not.

Therefore it is a good idea to get into the habit to investigate a few alternatives on how it can be represented.

Could you use a Python list instead?

Yes, you could keep a long list of all items sold during the day. Then at the end of the day, you could count the occurrences of each item in the list.

This would work too.

But it has some drawbacks.

  • You keep the same item multiple times, and it does not give any value here.
  • It will be more complicated to make a status of the sold items (you will see that in the end).

Hence we use a dictionary.

sold_items = {}

And surprisingly, we keep it empty.

Step 2 User prompt and update

Here you will see the power of using a dictionary.

But first, we need to prompt the user for what item and how many the user wants.

Afterward, we use the get method to update the dictionary.

Why, because get gives you the stored value or a given default value if the key does not exist.

item = input('What do you want to buy? ')
number_str = input('How many do you want? ')
number = int(number_str)
sold_items[item] = sold_items.get(item, 0) + number

Hence, the first time an item is looked up in the dictionary, it will return 0 and assign it to the number given. The second time, it will get the value and add number to it.

This actually fulfills the needs.

Step 3 Status of sold items

All you need is to print the key-value pairs from the dictionary.

for item, number in sold_items.items():
    print(item, number)

As you see, this step gets easy, because we used the dictionary as a data structure and not a list.

The full code

You have the full code here.

sold_items = {}
item = input('What do you want to buy? ')
number_str = input('How many do you want? ')
number = int(number_str)
sold_items[item] = sold_items.get(item, 0) + number
for item, number in sold_items.items():
    print(item, number)

Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment