Calculator
A menu in a loop: plus, minus, times, divide. Teaches menus, conditions and checking division by zero.
Open in Tokbit Level: Intermediate. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""A simple menu calculator. Teaches a menu loop and choosing with conditions."""
option: str = ""
a: float
b: float
while option != "5":
print("")
print("1 Addition (+)")
print("2 Subtraction (-)")
print("3 Multiplication (*)")
print("4 Division (/)")
print("5 Quit")
option = input("Choice:")
if option in ["1", "2", "3", "4"]:
a = float(input("a ="))
b = float(input("b ="))
if option == "1":
print("Result:", a + b)
else:
if option == "2":
print("Result:", a - b)
else:
if option == "3":
print("Result:", a * b)
else:
# Dividing by zero would stop with an error, so it is checked first.
if b == 0:
print("You can't divide by zero.")
else:
print("Result:", a / b)
else:
if option != "5":
print("Unknown choice.")
Follow-up tasks
Tasks in which you change the example (with a model solution):