Calculator Program in Python
In this lesson: Make a simple calculator in Python with functions and if-elif — add, subtract, multiply, divide — code with output.
What we are building
A calculator takes two numbers and an operator (+, -, *, /) and shows the result. It is a great first project that brings together input, conditions, functions and error handling. We will build it step by step and make it safe against divide by zero.
float(), or "2" + "3" gives "23" instead of 5.A basic calculator
a = float(input("First number: "))
op = input("Operator (+ - * /): ")
b = float(input("Second number: "))
if op == "+":
print(a + b)
elif op == "-":
print(a - b)
elif op == "*":
print(a * b)
elif op == "/":
print(a / b)
else:
print("Invalid operator")
Operator: +
Second number: 5
15.0
We read two numbers (converted to float) and an operator, then use if-elif to run the matching operation. else catches an invalid operator.
Using functions
Putting each operation in a function keeps the code clean and reusable.
def calculate(a, b, op):
if op == "+": return a + b
if op == "-": return a - b
if op == "*": return a * b
if op == "/": return a / b
return "Invalid operator"
print(calculate(20, 4, "/"))
Now the logic lives in one calculate function you can call anywhere, which is cleaner than repeating if-elif everywhere.
Handling divide by zero
def calculate(a, b, op):
if op == "/":
if b == 0:
return "Cannot divide by zero"
return a / b
# ... other operators
print(calculate(10, 0, "/"))
Dividing by zero would crash the program, so we check if b == 0 first. You could also use a try-except ZeroDivisionError block.
A menu loop
Let the user do many calculations until they choose to quit.
while True:
a = float(input("First number: "))
op = input("Operator: ")
b = float(input("Second number: "))
print("Result:", calculate(a, b, op))
again = input("Another? (y/n): ")
if again != "y":
break
print("Goodbye")
The while True loop repeats the calculator. After each result we ask if the user wants another; typing anything other than "y" breaks out and ends.
Common mistakes
- Not converting input.
input()gives a string — usefloat()orint()for maths. - Ignoring divide by zero. Always guard the division with a check or try-except.
- No
elsefor bad operators. Handle invalid input so the program does not fail silently. - Comparing with
=. Use==to compare the operator, not the assignment=.
Summary
- A calculator reads two numbers and an operator, then computes.
- Convert input with
float()before doing maths. - Use if-elif or a
calculatefunction to pick the operation. - Guard against divide by zero with a check or try-except.
- Add a
whileloop to let the user repeat.
Next, learn decisions in if-else in Python, or reuse code with Python functions.
Frequently Asked Questions
How do I take number input in Python?
input() to read text, then convert it with float() or int() — for example a = float(input("Enter a number: ")). Without converting, the input stays as a string and maths will not work.How do I handle divide by zero in Python?
Should I use if-elif or functions for a calculator?
How do I make the calculator repeat?
while True loop and offer an option to quit. After each calculation, ask if the user wants another, and break out of the loop when they choose to exit.Why does my calculator join numbers instead of adding?
float or int first so Python does real arithmetic.