Founding Batch Offer · HLD · LLD · System Design at ₹799 until 30 Nov

Menu-Driven Calculator in Python

Route a Python menu choice to addition, subtraction, multiplication, or division. New programmers trace text choices, numeric operands, and a zero-divisor guard.

BeginnerConditional ProgramsExample 16 of 20
menu-driven-calculator.py
1# Menu-driven calculator
2
3print("1. Addition")
4print("2. Subtraction")
5print("3. Multiplication")
6print("4. Division")
7
8choice = input("Enter choice (1-4): ")
9
10a = float(input("Enter first number: "))
11b = float(input("Enter second number: "))
12
13if choice == '1':
14 print("Result:", a + b)
15elif choice == '2':
16 print("Result:", a - b)
17elif choice == '3':
18 print("Result:", a * b)
19elif choice == '4':
20 if b == 0:
21 print("Cannot divide by zero.")
22 else:
23 print("Result:", a / b)
24else:
25 print("Invalid choice")

Output

Result: 15.0

Choice 1 selects addition, and 10.0 + 5.0 is 15.0.

In this one

  • Compare menu responses as strings
  • Route arithmetic through branches
  • Guard the divisor before /

How it works

  1. 1

    Print the menu and read choice without numeric conversion.

  2. 2

    Read a and b as floats.

  3. 3

    Select an operation, checking b before division, or print Invalid choice.

Edge cases

Division by zero

Choose 4 and enter b = 0.

The nested guard prints an error without dividing.

Unknown choice

Enter q.

After reading operands, the final else prints Invalid choice.

What's going on

A menu number comes in as text. choice is compared with '1', '2', '3', and '4', not integer literals. The two operands are separately converted to floats because their arithmetic needs numbers.

Each branch performs one operation. Division has a nested check: b == 0 prints a message instead of evaluating a / b. Python 3 / returns a float, so 10 / 4 produces 2.5 rather than discarding the fraction.

The last else catches unrecognized choices such as 5, q, or a blank response. Notice that this code still asks for both operands before validating the choice. It also runs only once; the printed menu is not a loop. Those are reasonable next improvements after the branch logic is clear.

After this program

If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why is choice compared with quoted digits?

Pick an answer to check it.

Question 2

Does division discard the fractional part?

Pick an answer to check it.

Questions people ask

Why is choice compared with quoted digits?

input returns a string, so the menu labels are matched as text.

Does division discard the fractional part?

No. Python 3 / performs true division and can return a fractional float.

Will the menu repeat automatically?

No. There is no enclosing loop, so the program handles one operation and ends.