What you'll build and why it matters
A calculator program is one of the best first projects in Python because it teaches you how functions work, how to take input from a user, and how to handle mistakes when someone types something wrong. You'll write a program that takes two numbers, asks which operation you want (add, subtract, multiply, or divide), and gives you the answer. The whole thing runs in about 30 lines of code.
This project is practical — you can actually use what you build — and it covers the core skills you'll need for bigger programs. You'll learn to organize code into reusable pieces, handle situations where things go wrong, and structure a program so it does one job well.
Key Takeaways
- A basic calculator needs a function for each operation (add, subtract, multiply, divide) and a main section that asks the user for input and calls the right function.
- The input() function collects what the user types, and float() converts text into a number Python can do math with.
- A try/except block stops your program from crashing when someone enters text instead of a number or tries to divide by zero.
- A while loop lets the user run multiple calculations without restarting the program each time.
- Testing your calculator with wrong inputs — like letters, zero in division, or negative numbers — catches bugs before you share it.
Set up your file and write the operation functions
Open a text editor or Python IDE (such as VS Code or IDLE, which comes with Python) and create a new file called calculator.py. Start by writing four functions, one for each operation. Each function takes two numbers as input and returns the result.
Type this into your file:
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y
Each function is simple — it takes two numbers and returns the result of one operation. You're building these separately so you can call them later without rewriting the math each time. The def keyword tells Python you're creating a function, and return sends the answer back to whoever called it.
Add a menu and get input from the user
Below your functions, add a section that shows the user what operations are available and asks them to pick one. This is where your program starts to feel like a real calculator.
print("Select operation.") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") choice = input("Enter choice (1/2/3/4): ")
The print() function displays text on the screen. The input() function pauses and waits for the user to type something, then stores what they typed in a variable called choice. At this point, choice is text (a string), not a number — that matters for the next step.
Convert input to numbers and handle errors
After you get the choice and the two numbers from the user, you need to convert them from text into actual numbers. This is where mistakes happen — if someone types "five" instead of "5", Python can't turn that into a number. A try/except block catches that mistake and tells the user what went wrong instead of crashing.
try: num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': if num2 == 0: print("Cannot divide by zero") else: print(num1, "/", num2, "=", divide(num1, num2)) else: print("Invalid input") except ValueError: print("Invalid input. Please enter numbers only.")
The try block contains code that might fail. The float() function converts text into a decimal number. If the user types something that isn't a number, Python raises a ValueError, and the except block runs instead, printing a friendly message.
The if/elif/else chain checks which operation the user picked and calls the matching function. Notice the division check: before you divide, you test whether num2 is zero, because dividing by zero breaks Python. If it is zero, you print a message. If it isn't, you do the division.
Wrap it in a loop so the user can do multiple calculations
Right now, your calculator runs once and stops. A while loop lets the user keep calculating without restarting the program. Add this around your entire input and calculation section:
while True: print("Select operation.") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") choice = input("Enter choice (1/2/3/4): ") try: num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': if num2 == 0: print("Cannot divide by zero") else: print(num1, "/", num2, "=", divide(num1, num2)) else: print("Invalid input") except ValueError: print("Invalid input. Please enter numbers only.") next_calculation = input("Do you want to do another calculation? (yes/no): ") if next_calculation.lower() != 'yes': break
while True means "run this block forever" — until you tell it to stop. At the end of each calculation, you ask the user if they want to continue. If they type anything other than "yes" (the .lower() part makes it work whether they type "yes", "YES", or "Yes"), the break statement exits the loop and ends the program.
Test your calculator with different inputs
Save your file and run it by opening a terminal or command prompt, navigating to the folder where you saved calculator.py, and typing python calculator.py. The program should display the menu and wait for input.
Test it with normal numbers first: try adding 5 and 3, subtracting 10 from 20, multiplying 4 by 6. Then test the edge cases — the situations where things can go wrong. Try dividing by zero (your program should say "Cannot divide by zero"). Try typing letters instead of numbers (the error message should appear). Try entering an operation number that doesn't exist, like 5. Each test confirms that your error handling works.
If something doesn't work the way you expected, read the error message Python prints. It usually tells you exactly which line has the problem. Common mistakes include forgetting the colon after if or while, indenting the wrong number of spaces (Python is strict about this), or mismatching quotes.
Frequently Asked Questions
Why do I need float() instead of int()?
float() handles decimal numbers like 3.5, while int() only handles whole numbers. Using float() makes your calculator more useful. If someone wants to divide 10 by 3, int() would give you 3, but float() gives you 3.333...
What's the difference between = and ==?
A single = assigns a value to a variable (like choice = input(...)). A double == compares two values to see if they're the same (like if choice == '1'). Using the wrong one is a common mistake that breaks your program.
Can I add more operations like square root or percentage?
Yes. Write a new function for each operation (like def square_root(x)), add it to your menu, and add a new elif block to call it. You'll need to import math at the top of your file to use math.sqrt().
Why does my program crash when I divide by zero?
If you're getting a crash, you probably don't have the if num2 == 0 check in your divide section, or it's in the wrong place. Make sure it's inside the elif choice == '4' block and before you actually call the divide() function.
How do I make the calculator look nicer?
Add more print() statements with blank lines or dashes to separate sections. You can also use print("\n") to add extra space. For a real graphical interface, you'd need a library like tkinter, but that's a separate project.