Python

Python Project: Store (5 Programming Skills You Need)

Implement a Simple Store in Python

In this project, you will implement a simple store in Python.

This is a great project that will teach how things can be easily modeled with Python built-in data structures.

You will learn the following.

  • Dictionaries. You will learn how to use Python dictionaries to keep track of what is sold, even though you don’t know what items you sell. This will teach you the flexibility you have using the built-in data structures.
  • User input. You will learn how to prompt the user for input.
  • Convert data types. You will learn how to convert strings to integers in Python. This is something will encounter often when programming.
  • Default values. You will learn how to use a dictionary and give a default value if the key does not exist already. This is by far one of my favorite things about Python dictionaries.
  • Iterate over the dictionary. You will also learn how to iterate over the key-value pairs in a dictionary.

Now this is great. Are you ready?

Project Description

You will start to sell items from the awesome store you implement in Python in this project.

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)

Want more Python projects?

This is part of 19 Python Projects and you can find ToDo list project and learn 5 essential skills.

Rune

Recent Posts

Build and Deploy an AI App

Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…

5 days ago

Building Python REST APIs with gcloud Serverless

Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…

5 days ago

Accelerate Your Web App Development Journey with Python and Docker

App Development with Python using Docker Are you an aspiring app developer looking to level…

6 days ago

Data Science Course Made Easy: Unlocking the Path to Success

Why Value-driven Data Science is the Key to Your Success In the world of data…

2 weeks ago

15 Machine Learning Projects: From Beginner to Pro

Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…

2 weeks ago

Unlock the Power of Python: 17 Project-Based Lessons from Zero to Machine Learning

Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…

2 weeks ago