Build a calculator that can add, subtract, multiply and divide, and that keeps going until you tell it to stop.
Computers are amazing at maths. They can add up millions of numbers in the blink of an eye. In this project you will turn Python into your very own pocket calculator. The player types two numbers and a symbol like + or *, and the calculator works out the answer.
Building a calculator is a great way to learn about functions. A function is a little machine with a name: you give it some numbers, it does a job, and it hands back a result. You will write one function for each kind of sum, and then your program will choose the right one based on what the player types.
You will also learn how to handle a tricky situation: dividing by zero! Real programmers always think about what could go wrong, and you will make sure your calculator gives a friendly message instead of crashing.
We create a function with the word def, give it a name, and list the values it needs in brackets. The return line sends the answer back to whoever called the function. Then we call add(3, 4) to test it.
def add(a, b):
return a + b
print(add(3, 4))
print(add(10, 25))
7 35
Subtracting, multiplying and dividing work in exactly the same way. In Python, * means multiply and / means divide. Notice that dividing always gives a decimal number, like 5.0, even when the answer is a whole number.
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
print(subtract(10, 4))
print(multiply(6, 7))
print(divide(20, 4))
6 42 5.0
Nobody can divide a number by zero, not even a computer! If you try, Python stops with an error. We add an if inside divide() to check whether b is 0 first. If it is, we return a friendly message instead of crashing.
def divide(a, b):
if b == 0:
return "Oops! You can't divide by zero."
return a / b
print(divide(9, 3))
print(divide(9, 0))
3.0 Oops! You can't divide by zero.
We use input() to ask for two numbers and a symbol. Because input() gives us text, we use float() to turn the numbers into decimals. That way the calculator works with numbers like 2.5 as well as whole numbers.
first = float(input("First number: "))
symbol = input("Choose + - * or /: ")
second = float(input("Second number: "))
print("You want to work out", first, symbol, second)
First number: 8 Choose + - * or /: + Second number: 5 You want to work out 8.0 + 5.0
Now we check which symbol the player typed and call the matching function. Each elif checks one more symbol. The final else catches anything we don't understand, like a typo. This piece of code uses the four functions from the earlier steps, so they need to be at the top of your file.
if symbol == "+":
answer = add(first, second)
elif symbol == "-":
answer = subtract(first, second)
elif symbol == "*":
answer = multiply(first, second)
elif symbol == "/":
answer = divide(first, second)
else:
answer = "I don't know that symbol."
print("Answer:", answer)
A real calculator doesn't switch off after one sum. We put everything inside a while loop that keeps going while keep_going is "yes". At the end of each sum we ask the player if they want another. Using .lower() means "YES" and "Yes" work too.
keep_going = "yes"
while keep_going == "yes":
# ...ask for the numbers and work out the answer here...
keep_going = input("Another sum? (yes/no): ").lower()
print("Thanks for calculating with me! đ")
Another sum? (yes/no): yes Another sum? (yes/no): no Thanks for calculating with me! đ
Here is the whole program with every step put together. If something isn't working in your version, compare it with this one line by line. Check the spelling, the colons at the end of if and while lines, and that the indentation (the spaces at the start of lines) matches.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Oops! You can't divide by zero."
return a / b
print("đ§Ž Mini Calculator")
keep_going = "yes"
while keep_going == "yes":
first = float(input("First number: "))
symbol = input("Choose + - * or /: ")
second = float(input("Second number: "))
if symbol == "+":
answer = add(first, second)
elif symbol == "-":
answer = subtract(first, second)
elif symbol == "*":
answer = multiply(first, second)
elif symbol == "/":
answer = divide(first, second)
else:
answer = "I don't know that symbol."
print("Answer:", answer)
keep_going = input("Another sum? (yes/no): ").lower()
print("Thanks for calculating with me! đ")
This is what it looks like when someone plays. The words after each question are what the player typed.
đ§Ž Mini Calculator First number: 12 Choose + - * or /: * Second number: 3 Answer: 36.0 Another sum? (yes/no): yes First number: 10 Choose + - * or /: / Second number: 0 Answer: Oops! You can't divide by zero. Another sum? (yes/no): yes First number: 7.5 Choose + - * or /: + Second number: 2.5 Answer: 10.0 Another sum? (yes/no): no Thanks for calculating with me! đ
Finished? Brilliant! Real programmers are always improving their programs. Try one of these challenges to make the project truly yours.
Stuck on a step? These lessons explain the Python ideas used in this project.