Read a working café till, change one thing and predict the result, then make a console app of your own with a menu, a checked number, a total and tests.
Every app you use is a menu, a check and a total, wearing a nicer coat. Today you build one.
This is the end of the console course. The program below uses everything: input, a checked number, while True, break, a running total and a function. Read it first, then make an app of your own.
Make your prediction first. Help opens once you've locked it in.
total = 0
while True:
choice = input("1 coffee $4, 2 cake $5, 3 pay: ")
if choice == "1":
total += 4
print("Coffee added. Total: $" + str(total))
elif choice == "2":
how_many = input("How many cakes? ")
if how_many.isdigit():
total += 5 * int(how_many)
print("Cakes added. Total: $" + str(total))
else:
print("Please type a number.")
elif choice == "3":
break
else:
print("Please choose 1, 2 or 3.")
print("Please pay $" + str(total))
Read the program.
You choose 1, then 2 with a quantity of 3, then 3. What total does the last line show?
Run it and compare with your prediction. Try a quantity that is a word.
total = 0
while True:
choice = input("1 coffee $4, 2 cake $5, 3 pay: ")
if choice == "1":
total += 4
print("Coffee added. Total: $" + str(total))
elif choice == "2":
how_many = input("How many cakes? ")
if how_many.isdigit():
total += 5 * int(how_many)
print("Cakes added. Total: $" + str(total))
else:
print("Please type a number.")
elif choice == "3":
break
else:
print("Please choose 1, 2 or 3.")
print("Please pay $" + str(total))
total = 0
while True:
choice = input("1 coffee $4, 2 cake $5, 3 pay: ")
if choice == "1":
total += 4
print("Coffee added. Total: $" + str(total))
elif choice == "2":
how_many = input("How many cakes? ")
if how_many.isdigit():
total += 5 * int(how_many)
print("Cakes added. Total: $" + str(total))
else:
print("Please type a number.")
elif choice == "3":
break
else:
print("Please choose 1, 2 or 3.")
print("Please pay $" + str(total))
Name the part that protects the program.
Work out a total by hand.
You choose 2 and type 2 as the quantity, and then choose 3. What is the total?
Change one thing at a time, predicting each result before you run it. Add a new item to the menu with its own price, and make pay choice 4.
Copy the cake branch and change the name and the price.
Add a line to the menu text so people know the new choice, and move pay to choice 4.
Add a new item to the menu with its own price and a checked quantity.
Run your program. This ticks when your code does all of the above.
Make a console app of your own. Pick a theme: a game shop, a canteen order, a fitness log or a unit converter. It needs:
while True) and a way to quit (break)+=)Then write three test cases for the part that works out replies, including one wrong input. Change one thing and predict what will happen before you run it.
You have finished the console course. Every Python extension starts from here.
Plan it on paper first: what are the choices, what is stored, when does it end?
Start from the café and change the theme, the prices and the replies.
A console app with a menu loop, a checked number, a running total and a way to quit.
Run your program. This ticks when your code does all of the above.
Students read a working café till, predict a change, then build a console app with a menu, a checked number and a total. About 45 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Planning before typing. Ask for the choices and what ends the program.
Stretch: Save the order to a list and print every item at the end.
Support: Give the café with one branch missing and ask for it.
The Python extensions all start here: objects, data detective, deeper console Python, and apps with Gradio.