#python
Let’s create a restaurant bill
- Cost of meal: $44.50
- Restaurant tax: 6.75%
- Tip: 15%
We will have apply the tip to the overall cost of the meal (including tax).
So let’s start by defining the variable “meal” and assigning it the value “44.50”
Now let’s create a variable for the tax percentage.
The tax on the receipt is 6.75%. Let’s divide 6.75 by 100 in order to get the decimal form of the percentage.
We assume we got good service, so we would like to leave a 15% tip on top of the cost of the meal, including tax.
Before we compute the tip for the total bill, let’s set a variable for the tip. Again, we need to get the decimal form of the tip, so we divide 15.0 by 100.
We’ve now got three variables we need to perform our calculation on, and we know some arithmetic operators that can help us out.
On Line 7, let’s reassign meal
to the value of itself + itself * tax to simplify it a bit
Now that the new variable of “meal"
has the cost of the food plus tax, let’s introduce on Line 8 a new variable, ”total"
equal to the new “meal"
+ ”meal
* tip"
So lets assign the variable “total"
to the sum of ”meal"
+ “meal
* tip"
on Line 8.
Also let’s define the final print value of the total with exactly two numbers after the decimal.
Compile this script and submit it…and you get your total bill.
Yes: 54.63
That’s how to write a script on Python to calculate your restaurant bill
This is me just writing to remember basic stuff am learning. Writing things I read helps me remember them better.
So now let’s start by explaining what we expect a simple calculator program to do in simple understandable english
I first read this, then tried it on DOS, typed this on IDLE and then just pasted it from there.
START PROGRAM print opening message
while we let the program run, do this: #Print what options you have print Option 1 - add print Option 2 - subtract print Option 3 - multiply print Option 4 - divide print Option 5 - quit program ask for which option is it you want
if it is option 1: ask for first number ask for second number add them together print the result on screen
if it is option 2: ask for first number ask for second number subtract one from the other print the result on screen
if it is option 3: ask for first number ask for second number multiply! print the result on screen
if it is option 4: ask for first number ask for second number divide one by the other print the result on screen
if it is option 5: tell the loop to stop looping
Print onscreen a goodbye message END PROGRAM
Pretty simple, where my simple calculator can only do 4 things
Add
Subtract
Multiply
Divide
Since anything beyond that right now is too much for my brain to capture and process
Now let's try writing it in a way that Python understands by defining functions and parameters
# calculator program # NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER # Here we will define our functions # this prints the main menu, and prompts for a choice def menu(): #print what options you have print "Welcome to calculator.py" print "your options are:" print " " print "1) Addition" print "2) Subtraction" print "3) Multiplication" print "4) Division" print "5) Quit calculator.py" print " " return input ("Choose your option: ") # this adds two numbers given def add(a,b): print a, "+", b, "=", a + b # this subtracts two numbers given def sub(a,b): print b, "-", a, "=", b - a # this multiplies two numbers given def mul(a,b): print a, "*", b, "=", a * b # this divides two numbers given def div(a,b): print a, "/", b, "=", a / b # NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN loop = 1 choice = 0 while loop == 1: choice = menu() if choice == 1: add(input("Add this: "),input("to this: ")) elif choice == 2: sub(input("Subtract this: "),input("from this: ")) elif choice == 3: mul(input("Multiply this: "),input("by this: ")) elif choice == 4: div(input("Divide this: "),input("by this: ")) elif choice == 5: loop = 0 print "Thankyou for using calculator.py!" # NOW THE PROGRAM REALLY FINISHES
That's it really.
Only 34 lines of code to make a calculator add/subtract/divide and multiply
And now I need to rest my overworked brain.
Thank you supremely intelligent denizens of the internet to make all this free.