Python

Build a calculator with Python

Start from zero. Write a calculator that takes your numbers and handles a bad input.

No coding experience needed4 steps
0/4 doneSaved in this browser
Start here

Before you start

A computer with Python 3 installed

The lessons and Python are free.

Download the starter
  1. 01

    Get comfortable with numbers

    Run a few small lines before building the whole program.

    VariablesBro Code

    Try it

    Set price to 12 and quantity to 3. Print their product. Then change the quantity to 5.

    The results should be 36 and 60. You should only need to change one value.

    See the example
    price = 12
    quantity = 3
    print(price * quantity)

    Python uses * for multiplication. Leave the numbers unquoted so they stay numbers.

    Watch lesson
  2. 02

    Use the numbers you type

    input() gives you text, even when you type a number.

    User inputBro Code

    Try it

    Ask for two numbers. Convert them with float(), then print their sum.

    Typing 2 and 3 should give 5.0, not 23. Try 2.5 and 3 as well.

    Why did I get 23?
    first = float(input("First number: "))
    second = float(input("Second number: "))
    print(first + second)

    Adding two strings joins them. float() turns the typed text into numbers. A word such as hello will still raise ValueError; handle that in the final step.

    Watch lesson
  3. 03

    Choose an operation

    Keep the choices small: +, -, * and /.

    Try it

    Ask for an operator. Use if and elif to choose the calculation. Add an else for anything you do not support.

    8 * 3 should give 24. An operator such as % should show your unsupported-operation message.

    Handle division by zero
    if operator == "/":
        if second == 0:
            print("You can't divide by zero.")
        else:
            print(first / second)

    Check the second number before dividing. Do not replace it with 1 to hide the error, because that changes the calculation.

    Watch lesson
  4. 04

    Put it together and test it

    I'd change the inputs now. Copying the result from the lesson won't tell you much.

    Try it

    Combine your inputs and operator choices. Catch ValueError for a bad number. Let someone run it without explaining each line.

    Try every case in the checklist below. Fix one failure at a time.

    Catch a bad number
    try:
        number = float(input("Number: "))
    except ValueError:
        print("Please enter a number, like 2 or 2.5.")

    This catches the conversion error. To ask again, put the input in a loop and leave the loop after a valid number. The course's Exception handling lesson explains the pattern in more detail.

    Watch lesson

Check your result

Your calculator should work with more than the numbers from the video.

  • 12 + 7 gives 19.
  • 7 divided by 2 gives 3.5.
  • Dividing by zero shows a message instead of crashing.
  • Typing a word asks for a number again.

What to do next

Save your calculations to a file, then read them back.

Read and write files

Practice by Abdulrhman Sayed. Video lessons belong to the credited creators.