Coding: Creating a Simple Calculator

Published: 2025-08-21 Coding: Creating a Simple Calculator
For kids

Learn how to build a basic calculator using a programming language called Python! You'll be able to add, subtract, multiply, and divide numbers with your own calculator.

What You'll Need

  • A computer with a web browser.
  • Access to a website where you can write and run Python code (like a free online Python interpreter, you may try "https://www.online-python.com/").
  • A little bit of patience!

Let's Code!

We're going to build our calculator step-by-step. Don't worry if you don't understand everything at first; that's okay! Just try your best, and you'll learn as you go.

  1. Choose Your Numbers: First, our calculator needs to know the numbers to work with. We will ask the user to input two numbers.
  2. Choose Your Operation: Next, we'll ask the user what they want to do with those numbers (add, subtract, multiply, or divide).
  3. Do the Math: The calculator will then perform the calculation based on what the user chose.
  4. Show the Answer: Finally, the calculator will show the user the answer!

Here is the Python code for your calculator. You can copy and paste this into your online Python interpreter and run it.


# Get the first number from the user
num1 = float(input("Enter first number: "))

# Get the second number from the user
num2 = float(input("Enter second number: "))

# Ask the user for the operation
operation = input("Enter operation (+, -, *, /): ")

# Perform the calculation
if operation == '+':
    result = num1 + num2
elif operation == '-':
    result = num1 - num2
elif operation == '*':
    result = num1 * num2
elif operation == '/':
    if num2 == 0:
        result = "Error! Cannot divide by zero."
    else:
        result = num1 / num2
else:
    result = "Invalid input"

# Display the result
print("Result:", result)

How to Use Your Calculator

After you run the code, the calculator will ask you some questions. Here's what you'll do:

  • The calculator will ask you to "Enter first number:". Type in your first number and press Enter.
  • It will then ask you to "Enter second number:". Type in your second number and press Enter.
  • Next, it will ask you to "Enter operation (+, -, *, /):". Type in the symbol for the operation you want to do:
    • + for addition
    • - for subtraction
    • * for multiplication
    • / for division
    Press Enter.
  • The calculator will then show you the answer!

Try This Next!

  • Try adding more operations, like exponents (using **).
  • Make the calculator ask if the user wants to do another calculation after showing the result.
  • Add error handling to catch invalid inputs, like text instead of numbers.

Troubleshooting

  • The calculator isn't working! Double-check that you typed the code exactly as shown. Even small mistakes can cause problems.
  • I'm getting an error! Read the error message carefully. It will tell you what went wrong. Common errors include typos or incorrect indentation.
  • The answer is wrong! Make sure you entered the numbers and operation symbols correctly.

You did it! You've built your own calculator. Coding is like solving puzzles, and it's awesome that you're learning. Keep practicing, and you'll get even better!