“CREATING A MENU”-: An Online Chocolate Store — #LearningPythonThroughProjects Series 5
Welcome to another awesome week of coding! This is the 5th series and we are gradually making progress. So far, we have explored some basic concepts in Python. In the previous two series, we explored the use of Flow control statements(Conditional and iterative). For the next couple of series, we will explore a variety of cool projects that can be built using these concepts. This is because it is very important to get a solid understanding of these concepts, and what better way than to solidify by exploring more projects.
In this project, we will be building a mini online ‘Chocolate Shop’, well not exactly online in the real sense, since it is not going to hosted on a site or something. The idea is rather to picture trying to purchase an item, develop the algorithm(steps) and flowchart around the problem, then deploy and translate to code. After all, being able to break problems into minute steps and processes before building it code-base makes a “better programmer”.
Ideally, when you visit a store, in person or online, you have a range of products to choose from(a menu), sometimes you might already have a product in mind you wish to purchase, so you direct your search to that single product and if it is not available, you either view similar products or you exit the store(or website) and try some other places. If the product of choice is found, you move on to the counter for payment or get redirected to a payment merchant. Using this scenario, with the focus on “Online experience”, we will be building a simple program. A menu will be created, with options provided to the user to select from, then Conditional Statements will be used to check if the selected option by the user is available or not.
Let’s get into it!
AN ONLINE CHOCOLATE SHOP
STEPS:
- Open a New Python file, save it as ‘chocolateshop.py’
- Then create a Welcome message for the Shop, ask the user for a name, and Print a greeting message welcoming the user to the online shop.
- Next, create a menu. For the menu, you will learn how to create options.
Use the backslash on the keyboard ‘\’.
Note that ‘/’ and this ‘\’ are not the same. ‘/’ is called the forward-slash (the explanation of this and its use was shared in previous series- “A simple calculator project”), and ‘\’ is called the backslash. The two have different functions in Python.
Let’s see how a menu option can be created using the \ backslash in Python.
So a menu with different types of chocolates will be created as shown below: - Next, create two notes for the user after he/she makes a choice from the options.
- Use the conditional statements to make decisions for whichever option the user chooses, as shown below. Do not worry much about the extra method added, as always, it will be explained once the program has been executed successfully.
- Finally, create a last print statement outside the conditional block as a final goodbye message.
Great! The program is complete. Check that all syntax has been correctly typed before running to avoid errors.
Fun Fact: Errors are also known as Bugs in Programming, and the process of solving/Correcting an error is known as Debugging.
Nice! A bit less complicated than the previous series. There are some new concepts introduced, Let’s explain that.
PYTHON CONCEPTS USED
- The print Statement
- The Input Statement
- The Escape character (Backslash \)
- The ‘\n’ — newline character
- The ‘.lower()’ method
- The escape character and the ‘\n’ newline character: So earlier, in the first series, I explained that in Python, while creating possessive pronouns, double strings are used to enclose the strings and then a single string used in the word where possessiveness has to be shown.
There is also another way of doing this, using the escape character(backslash \) in Python. This special character is used for escaping quotes in Python, it automatically indicates that the string continues and that extra quote goes along with the word. So the sentence above can also be printed in the following way:
This unique character can also be used in many other ways. When used with the letter ’n’ as ‘\n’, it indicates a new line, i.e the program automatically moves any statement after the declaration of the character ‘\n’, to a new line.
As seen in the program earlier, every statement after the declaration of the character was printed in a new line on execution.
For more insights on its usage, please reference the documentation links below.
2. The .lower() method on strings to convert all alphabets to lower-case. So if for example, we have a string that says message =‘Hello World’, applying the .lower() method converts all alphabets to lower case and returns the output as message = ‘hello world’.
So in the project earlier, we were simply applying the .lower() method to the user’s response in such a way that no matter what case was used by the user, It is automatically converted to a lower case which eventually becomes equivalent to the condition set in the conditionals. This is just another way of preventing unnecessary errors while building a program. Since Python is a case-sensitive programming language, taking care of these little things just makes life easier and ‘Bug-Free’.
There are several other string methods that could have been used here as well, such as :
- The .upper() — for converting strings to upper case
- The .capitalize() — for converting only the first letter in a string
- The .title() — Converts the first character of each word to upper case. It is quite different from the .capilalize(). Take a look at an example below:
Notice, the difference in the methods.
- The .casefold() — acts just like the .lower() method, it converts a string to all lowercase.
- The .swap() — just as in the name, it swaps cases. So if the string was initially in lower case, it swaps it to upper case and vice versa.
There are several other string methods, reference the links below for further studies.
END NOTE
Thank you for tuning in once again! As mentioned earlier, the next couple of projects will focus on projects the use of Flow control statements. The aim is to get to a good grasp of these concepts
As always the link to the GitHub repository has been included below, Also, the links to the previous series.
LINKS TO PREVIOUS SERIES IN ORDER FROM FIRST TO LATEST
EXTERNAL LINKS AND RESOURCES