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.
Now this is great. Are you ready?
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.
apple
pear
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.
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.
Hence we use a dictionary.
sold_items = {}
And surprisingly, we keep it empty.
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.
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.
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)
This is part of 19 Python Projects and you can find ToDo list project and learn 5 essential skills.
Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…
Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…
App Development with Python using Docker Are you an aspiring app developer looking to level…
Why Value-driven Data Science is the Key to Your Success In the world of data…
Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…
Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…