So let's get started. For this code, I imagine we would like to open our own ice-cream store. However, due to the lack of manpower, we would like to create a program that enables us to calculate the total cost of our customers' order. Since we just started programming, this code is not something we can really use in an enterprise, however, it is a code that aids in our understanding of how to do calculations with user's inputs.
For this particular tutorial, I will be using a script and for those of you who don't have a text editor you may download one from these links: http://notepad-plus-plus.org/ for Windows and http://brackets.io/ for Mac. If you prefer other text editors that your friends or tutors have recommended to you, it will work fine as well. Once you have your text editors downloaded, you have to execute them in order to type your Python code in. Please note that when you save your code, be sure to save it in python. This means your file has to end with .py, and in this case you can see it on the top left hand corner that I have saved my file as "Icecreamshop.py".
Before we try to execute our code, let's go through it once and learn what each section of the code means. I have included some comments in the code above so that you can read and understand each section better. The comments are all in green. You may or may not want to include the comments in your program as it will not affect how your programs work. Comments are just there for you to elaborate more on your code so that others who might be reading your code will understand them better. They are indicated after "#", anything that is typed after "#" is ignored by the Python program.
For the first section of our code, as seen from the above, it's quite straight forward. Before we can calculate our cost, we need to let our customers know what is available in our ice-cream store. For illustrating purposes, let's just assume we have only three flavors of ice-creams to sell. So we just print out our menu for our customers to decide what they would like to order. If you don't like the way I introduce my menu to the customers, you may change the strings with the inverted commas. Perhaps, you can phrase it is a much interesting way compared to mine. It will still work fine.
Suppose we are able to run the first part of our code, the result would be something like the above. Do note that when we print on the next line, the strings after print will appear on the next line as well. You may try to key in something like: "print 'Welcome to our Ice-cream shop!', 'Hello!' " and see the results for this. It should appear on the same line. That's is pretty much all for the first part of the code. Now, we will move on to the slightly more complicated part which involves having our users to input the quantity of ice-creams they would like to order.
Now, relax and we will take our time to understand this part of the code. In order to allow for user to input an information, we will need the "raw_input()" function which you can see from the above. For the first line, "inp = raw_input('How many Vanilla ice-cream would you like?')" would prompt the program to print a string which ask how many Vanilla ice-cream your customer wants to order and allow your customers to type a number into the program. Then, the second line "v = int(inp)" means we take the input from our customer and convert the input into an integers and assign it to the variable v. So 'v' would store the number of vanilla ice-cream our customer has ordered. It's only logical that the information that your customer key in would be an integer as it represents the amount of ice-cream. However, there are always users that key in something else apart from integers and this will result in an error for our program. For now, we have not learn anything to deal with such errors, however, we will try how to deal with in the later tutorials that I would post sometime this coming week or next week if I am too busy. We repeat the codes for Chocolate ice-cream and Mint ice-cream as well, and we will have the amount that the users key in stored in 'c' and 'm' respectively.
If we were to run only section two of our code, we will have the results to be something like this. The integers highlighted was keyed in on my own. In this case, I chose zero vanilla ice-creams, two chocolate ice-creams and two mint ice-creams. You may choose to key in other integers of your choice, it should work fine as well.
The result for our last bit of code should look something like the above. Of course, if you don't like how I phrase it, you may change the strings to phrase it different. But make sure that you use the variables that you assigned to the customer's inputs to do the calculation for the total cost. If you don't, if will results in an error.
So, after we have understand each part of our code, we will need to learn how to execute our script. Since I am using Windows I can only illustrate the example using windows. For Windows users, you can run command prompt by typing 'cmd' into the find option in your start windows or go to 'Accessories' and select 'command prompt'. After which, you will need to locate your python file, it will depend on the location in which you save your file in.
In my case, I save mine in a folder in desktop so after I open command prompt, I will need to move to my desktop so I will need to key "cd Desktop". Then choose the folder by typing, "cd Example of Python code" and execute the file in Python by typing out the file name in full "Icecreamshop.py". Please note that do not include spaces to your python file name as it will results in an error in Windows command prompt. Then, it will execute your python script in full as seen from above.
This marks the end of our very long Python tutorial today. As usual, feel free to explore and play around by editing and changing some parts of the code to familiarize yourself with it. After which, you may wish to write one from scratch yourself to make sure that you understand the logic behind the program. If you have any question, feel free to ask and post a comment. I will try my best to answer them as long as it is relevant to the tutorials that I have done up. Also, if you happen to spot any mistakes, kindly drop me a comment as well so that I can amend my mistakes. Thank you so much for reading and enjoy programming! Ciao.
No comments:
Post a Comment