“CHOCOLATE STORE UPDATED”: #LearnPythonThroughProject Series 6
Do you love chocolates? I do too!! In this article, we will be revisiting the chocolate store created in the previous article(link here(hover your mouse around this text to view link)). We will be adding new features such as a shopping cart , to improve the functionality of the ‘Chocolate Store’.
One very important thing I would like to emphasize in this project is the importance of ‘Pseudocodes’ and Algorithms. A Pseudocode is simply the informal description of a process or a program. It is a step-by-step outline of how a code/program should run without strict programming syntax. It is quite different from an algorithm. While an algorithm involves detailed instructions, with sequences, selections, and possible iterations, in an orderly format for a computer program, a Pseudocode is rather simpler with short phrases used. It is a way of representing an algorithm. Both are interrelated.
Take a look at the image below, while the table at the right shows a sample algorithm, the table at the left is a Pseudocode. Observe closely and note the difference.
So in this project, we are first going to break the project into smaller steps, then write out the code for it. This just makes it easier to deploy a problem into code. This is because a thought-process has already been done with instructions clearly written out, Hence translating to code becomes less of a hassle.
“Think about a time when you carefully thought about a problem, a plan, an idea or a goal, you penned down steps and procedures to be taken to achieve the results you desired. Hence it was structured, more doable and easier to achieve, than just randomly taking steps. “The Divide and conquer rule”. Try this with a problem you want to solve using your programming skills.
As always, we will be building the project first, then explain the steps and concepts used.
PSEUDOCODE FOR PROJECT
Let’s go through this together:
STEPS: (Do not forget to create a new file and save the name with a .py extension)
- Initiate a welcome message
2. Ask User for name
3. Print User’s name
4. Print an empty line and some information.
5. Initiate a dictionary data structure for items, price, and quantity
6. Iterate through dictionary and print Keys and Values for each item in the dictionary.
7. Print an empty line
8. Initiate a shopping cart using a list data structure.
9. Initiate the order process. Print items in a multi-line string format.
10. Print items
11. Print an empty line
12. Get the user’s order using the input function
13. Initiate a while loop to check if the order in the listed item.
14. Append order to cart
15. Ask the user for more item
16. Initiate an inside while loop to validate user’s response
17. Append order to cart
18. Else close shopping with multiple print statements
19. Break Loop to exit
Great! The program is complete. Let’s test it!
Works Perfectly!
PYTHON CONCEPTS USED
- Print Statement
- Input Statement
- Python Data Structures (Dictionary and List), and methods(append)
- Iterative Statements(While and For Loop)
- Python Multiple line texts
- Conditional Statements(Else)
- Transfer Statements(break)
So we are already familiar with most of these concepts. I will move on to explaining the newly introduced.
- Python Data structures(Dictionary and List) and methods: In a previous series(series 4), the concept of data structures in Python was introduced, We explored the use of tuples and lists. In this project, the Dictionary data structure was used in creating the menu option. Now you might want to ask if the tuple or list can be used for the same purpose? Definitely! The difference is with the dictionary, the menu content can easily be accessed using the keys. Remember that Python dictionary works with ‘Keys:Value’, where for each key, there’s a value pair. The perks of using the dictionary for this purpose is that we can store multiple key values in one key. What do I mean?
Take a look at this dictionary example:
Values stored under each key could easily be called just by referencing the key names as follows:
We need not print ‘Age and the value together’, for what we seek is just the value attached to the key. This is what makes Python Dictionaries so spectacular.
Now in this project, the ‘Menu dictionary’ was created using multiple key values attached to just one key.
How was that done?
So ideally, one key should be attached to one value as a pair, however, Python provides us options to attach multiple key values to one key name, using a trick. The trick is enclosing the multiple values in either a List(using square brackets []) or a Tuple(using parenthesis ()), with values separated by a comma, and sticking to the format for each data type(Quotes for string values).
How then do we access these multiple values using the key name?
There are several methods of doing do in Python. If the key name is called alongside the dictionary name, all multiple values get printed out. Check out the following methods of achieving this.
What if we wanted a specific value from the sets of multiple values, how can this be achieved? Well, We can do this using indexing. Remember the multiple values have been stored using either Lists or Tuples, and for these data structures, items can be accessed using indexing(Position of items starting from 0 for the first item). Let’s see how that works!
So what happened here? We simply called out the dictionary name, the key name(take note of this, the key name was used not index position, remember that the key name has numeric values in this example), and finally, the index position of the item in the Tuple.
The syntax is: “dictname[keyname][index position in list or tuple]”
Fun fact: “In this way, a mini database can be created with pairs of values stored under one key”
Also in this project, an empty list was initiated and then values added as the program runs using the “.append” method. This method works by adding values to the end of the list. Take note that in the program, items were added to the cart always at the end, accordingly. So for every time the loop runs, the entered item by the user is automatically stored in a list, ‘the shopping_cart list’, with each value added at the end of the list. The .append method is particular to just the Python list. Tuples are immutable and Dictionaries have a special method for adding values. There are several other list methods such as .extend(), .insert() et cetera. Reference the links below for further study.
- Iterative Statements(While and For Loop): In this project, we explored the use of the while and for loop in initiating an iterative process. These two iterative statements, however, works differently. One is an infinite process and keeps running for as long as a condition holds True, whereas the other stops running after a set interval. To understand better the difference between these two, take a look at the example below:
The While loop keeps running for as long as the condition set(hobby in list) holds True. Remember how to stop a while loop?
Let’s see what the For loop will do:
Did you see that? The for loop iterated for only the number of items in the list(3). Awesome right!
So the choice on which of these to use is dependent on the purpose(s) of the iteration. While iterating through the ‘menu’ option in the project, the for loop was chosen for the items in the dictionary to get printed just once and the process exited because a continuous/infinite process is not required here. Whereas for the cart option, the while loop was used such that as long as the user wants to keep adding items to the cart(keep shopping), and the item is available(in the menu), the process is kept running.
We will explore more use of the ‘for loop’ in future projects. Stay tuned!
- Multiple line texts/Strings: In Python, multiline texts/strings can be initiated using the triple quotes, either single(‘’’) or double(“””)[remember to stick to just one of the syntax(single or double) at the beginning and at the end]. This is very useful for a sentence or a long text. Python recognizes the start of the string from the first triple quotes and the and as the last triple quotes.
- Transfer Statements in Python: Recall that in Python we have three(3) types of Flow control Statements; Conditional(if-else-elif), iterative(while-for) and Transfer statements(break-continue-pass).
They are called control statements because they “control a process”
Transfer Statements change the regular flow/execution of a program.
While the ‘break’ exits a loop by taking the control of the program outside the loop and executing other commands below it(if any), the ‘çontinue’ statement returns the loop to the beginning of the program, and finally ‘pass’ is an empty control which executes without doing the actions listed in the loop.
In this project, the ‘break’ statement was used to exit the program from the loop, since a while loop was used, it would have kept running infinitely( we would not want that right!).
This was just an introduction to the use of these statements. More applications in future projects.
END NOTE
Once again, we built a really cool project. Imagine more features been added and this turns into one mindblowing program. We are gradually making progress. One more brick stone of success cemented. Great Job!
Thank you for tuning in. If you have not seen the previous series find the links in my profile. Also find the link to Github repository for the codes used in this project, as well as links to useful resources.
CONNECT ON SOCIAL MEDIA
Twitter: @diyyah92
LinkedIn: https://www.linkedin.com/in/aminah-mardiyyah-rufa-i/