Menu Calculator
Programs with input · Intermediate · about 8 min
Read a menu choice, then two whole numbers, one per line. The choices are:
- 1 adds the two numbers
- 2 subtracts the second number from the first
- 3 multiplies them
- 4 divides the first number by the second
Print the answer as Result: 15. For a division, show 2 decimal places, as Result: 3.50, and if the second number is 0, print Cannot divide by zero instead. Any other choice prints Invalid choice. Always read all three lines, even when the choice is invalid.
You can show the menu inside the first prompt: the prompts inside input() are not checked, only what you print.
Examples
You type:
1 10 5
The program prints:
Result: 15
You type:
4 7 2
The program prints:
Result: 3.50
choice = input("1 Add, 2 Subtract, 3 Multiply, 4 Divide: ")
a = int(input("First number: "))
b = int(input("Second number: "))
# Print the result for the choice
The solution opens after your first Check.