🎉 Welcome to PyVerse! Start Learning Today

Simple Calculator Program

Basic
Python

Build a functional calculator in Python

1) Project Overview

This project is a Simple Calculator you run in the console. It lets you:

  • Choose an operation: add, subtract, multiply, or divide.
  • Enter two numbers.
  • See the result.
  • Keep calculating until you choose to quit.

It also protects the program from crashing if you type something that's not a number, and it warns you if you try to divide by zero.

2) Learning Objectives

By the end, students will be able to:

  • Use input() and print() for user interaction.
  • Store data in variables and work with numbers (floats).
  • Use basic math operators: +, -, *, /
  • Make decisions with if, elif, else.
  • Repeat actions with a while loop and stop with break/continue.
  • Handle errors (like non-number input) using try and except.
  • Organize code with a small helper function.

3) Step-by-Step Explanation

  1. Plan the features:
    • We need a menu to choose the operation.
    • We need to ask for two numbers.
    • We need to calculate and show the result.
    • We should repeat until the user quits.
    • We should handle mistakes (wrong menu choice, not-a-number input, divide by zero).
  2. Build a helper function get_number(prompt):
    • Asks the user for a number.
    • If they type something invalid, ask again politely.
  3. Show the menu inside an endless loop (while True):
    • Print the options 1–4 and Q to quit.
  4. Read the user's choice:
    • If it's Q (or q), say goodbye and break the loop.
    • If it's not 1–4, show a helpful message and continue to the next loop round.
  5. Ask for the two numbers using get_number() so the program doesn't crash on bad input.
  6. Do the math:
    • Use if/elif to decide which operation to run.
    • If dividing, check if the second number is zero; if yes, warn and skip that calculation.
  7. Show the result in a clear format, like: 8.0 / 2.0 = 4.0
  8. Loop back to the menu so the user can calculate again.

4) Complete and Well-Commented Python Code

# Simple Calculator Program # Grade 8–9 friendly version def get_number(prompt): """ Keep asking the user for a number until they type a valid one. Returns the number as a float. """ while True: text = input(prompt) try: # Try to convert the text to a floating-point number value = float(text) return value except ValueError: # If conversion fails, tell the user and ask again print("Oops! That is not a number. Please try again.") print("Welcome to the Simple Calculator!") print("You can add, subtract, multiply, and divide two numbers.") # Main loop: show the menu again and again until the user quits while True: print("\nWhat would you like to do?") print("1) Add (+)") print("2) Subtract (-)") print("3) Multiply (*)") print("4) Divide (/)") print("Q) Quit") choice = input("Choose 1-4 or Q to quit: ").strip().lower() # If the user wants to quit, break the loop if choice in ("q", "quit"): print("Goodbye! Thanks for using the calculator.") break # Check if the choice is valid if choice not in ("1", "2", "3", "4"): print("Please choose 1, 2, 3, 4, or Q.") continue # Go back to the start of the loop # Get two numbers safely using our helper function a = get_number("Enter the first number: ") b = get_number("Enter the second number: ") # Decide which operation to perform if choice == "1": result = a + b symbol = "+" elif choice == "2": result = a - b symbol = "-" elif choice == "3": result = a * b symbol = "*" else: # choice == "4" # Prevent dividing by zero if b == 0: print("You cannot divide by zero. Please try a different second number.") continue result = a / b symbol = "/" # Show the calculation and the result print(f"Result: {a} {symbol} {b} = {result}")

5) Output Examples

Example 1: Normal addition

Welcome to the Simple Calculator!
You can add, subtract, multiply, and divide two numbers.

What would you like to do?
1) Add (+)
2) Subtract (-)
3) Multiply (*)
4) Divide (/)
Q) Quit
Choose 1-4 or Q to quit: 1
Enter the first number: 12.5
Enter the second number: 3
Result: 12.5 + 3.0 = 15.5

Example 2: Invalid menu choice

What would you like to do?
1) Add (+)
2) Subtract (-)
3) Multiply (*)
4) Divide (/)
Q) Quit
Choose 1-4 or Q to quit: 9
Please choose 1, 2, 3, 4, or Q.

Example 3: Not-a-number input

Choose 1-4 or Q to quit: 3
Enter the first number: ten
Oops! That is not a number. Please try again.
Enter the first number: 10
Enter the second number: 2
Result: 10.0 * 2.0 = 20.0

Example 4: Division by zero

Choose 1-4 or Q to quit: 4
Enter the first number: 8
Enter the second number: 0
You cannot divide by zero. Please try a different second number.

6) Small Exercise

Challenge: Add a new operation for exponent (power)

Goal: Add option 5) Power (^) that calculates a to the power of b using ** operator.

Hints:

  • Add a new menu line for option 5.
  • Accept choice "5" and compute result = a ** b.
  • Show the result like: a ^ b = result

Optional extra: Keep a history list of results and add a menu option H) to display the last 5 calculations.

7) Summary

Great job! You built a working calculator that talks to the user, handles mistakes kindly, and performs real math. You practiced inputs, decisions, loops, and error handling—core skills for any programmer. Keep experimenting by adding new features. Every small improvement makes you a stronger coder!