← All coding practice

Coding Practice · Beginner · October 8, 2026 · 3 min read

Python Coding Exercise #16: Calculator

Part of the series Python Practice Exercise — part 16

Before you start: Know input() and int() · Know if statements and f-strings

The question

Please write a program which asks the user for two numbers and an operation. If the operation is add, multiply or subtract, the program should calculate and print out the result of the operation with the given numbers. If the user types in anything else, the program should print out nothing.

Some examples of expected behaviour:

Number 1: 10
Number 2: 17
Operation: add
10 + 17 = 27
Number 1: 4
Number 2: 6
Operation: multiply
4 * 6 = 24
Number 1: 4
Number 2: 6
Operation: subtract
4 - 6 = -2

My Solution

number1 = int(input("Number 1: "))
number2 = int(input("Number 2: "))
operation = input("Operation: ")

if operation == "add":
    print(f"{number1} + {number2} = {number1 + number2}")
if operation == "multiply":
    print(f"{number1} * {number2} = {number1 * number2}")
if operation == "subtract":
    print(f"{number1} - {number2} = {number1 - number2}")

How My Solution Works

1. User inputs

The program reads two numbers and converts each with int(). The operation is different: it’s a text/string such as add, so it stays a string and doesn’t need converting.

2. if statements

The three if statements compare the typed text with ==:

  • operation == "add" is true only if the user typed exactly add.
  • operation == "multiply" is true only for multiply.
  • operation == "subtract" is true only for subtract.

Only the matching if runs. Its f-string builds the equation: the symbols +, * and - are just fixed text typed into the string, and the answer is calculated inside the curly braces, for example {number1 + number2}. For 10 and 17 with add, the output is 10 + 17 = 27.

3. Doing nothing for anything else

The exercise says that for any other input the program should print nothing. There is no else branch, so when none of the three words match, no print() runs and the program ends. The program prints nothing for divide, for an empty answer, and even for Add with a capital letter, because string comparison is case-sensitive (to ignore the case, see String Methods in Python).

(An else: print() would not be “nothing”. As we saw in Exercise #13, an empty print() still outputs a blank line.)

University Solution

number1 = int(input("Number 1: "))
number2 = int(input("Number 2: "))
operation = input("Operation: ")

if operation == "add":
    print(f"{number1} + {number2} = {number1 + number2}")
if operation == "subtract":
    print(f"{number1} - {number2} = {number1 - number2}")
if operation == "multiply":
    print(f"{number1} * {number2} = {number1 * number2}")

The University Solution is almost the same as My Solution. It checks the operations in a different order (add, subtract, multiply), but the order doesn’t matter here, because only one of the three conditions can be true for any answer.

Key takeaways

  • Strings are compared with ==, and the comparison is case-sensitive.
  • Only the if whose condition is true runs its indented lines.
  • With no else, a program does nothing when no condition matches.
  • Use elif when exactly one of several options can apply. Use separate ifs when more than one can be true.
  • Numbers are converted with int(). Words stay as strings.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · conditionals · strings · f-strings · arithmetic