← All coding practice

Coding Practice · Beginner · October 7, 2026 · 4 min read

Python Coding Exercise #10: Fix the Code (Product)

Part of the series Python Practice Exercise — part 10

Before you start: Know input() and int() · Know what a variable is

The question

This program asks the user for three numbers. The program then prints out their product, that is, the numbers multiplied by each other. There is, however, something wrong with the program. It doesn’t work quite right, as you can see if you run it. Please fix it.

An example of the expected execution of the program:

Please type in the first number: 2
Please type in the second number: 3
Please type in the third number: 5
The product is 30

Here is the code to fix:

number = int(input("Please type in the first number: "))
number = int(input("Please type in the second number: "))
number = int(input("Please type in the third number: "))

product = number * number * number

print("The product is", product)

What the program prints now

Run with the numbers 2, 3 and 5, the program prints:

The product is 125

That’s the wrong answer (2 × 3 × 5 is 30), but there’s no error message. This kind of bug, where the program runs fine but gives a wrong result, is called a logic error, and it’s the hardest kind to spot, because Python has no way of knowing what you meant to calculate.

Why it prints 125

The program uses only one variable, number, to store all three inputs. A variable holds one value at a time, so each new assignment replaces whatever was stored before. Here’s what number contains after each line:

After this lineValue of number
number = int(input("...first number: ")) with input 22
number = int(input("...second number: ")) with input 33 (the 2 is replaced)
number = int(input("...third number: ")) with input 55 (the 3 is replaced)

By the time the calculation runs, number is just 5. So number * number * number becomes 5 * 5 * 5, which is 125. The first two numbers the user typed were replaced.

My Solution

Give each input its own variable:

number1 = int(input("Please type in the first number: "))
number2 = int(input("Please type in the second number: "))
number3 = int(input("Please type in the third number: "))

product = number1 * number2 * number3

print("The product is", product)

Output:

Please type in the first number: 2
Please type in the second number: 3
Please type in the third number: 5
The product is 30

How My Solution Works

With three different names, nothing gets overwritten. After the three inputs, the program remembers number1 = 2, number2 = 3 and number3 = 5, and the calculation number1 * number2 * number3 becomes 2 * 3 * 5, which is 30.

The last line uses a comma: print("The product is", product). A comma in print() puts a space between the two values, so the output reads The product is 30 with exactly one space. The result is a number, but print() converts it to text itself, so no str() is needed. (More about this in Exercise #05.)

How to find a bug like this

When a program gives the wrong answer without any error, a good habit is to print the variables at each step and compare them with what you expected:

number = int(input("Please type in the first number: "))
print(number)       # 2, as expected
number = int(input("Please type in the second number: "))
print(number)       # 3 — but where did the 2 go?

Seeing the first value disappear points straight at the problem. You can delete the extra print() lines once the bug is fixed.

It also helps to ask a simple question: “how many separate values does this program need to remember at the same time?” Here the answer is three, so the program needs three variables.

Key takeaways

  • A variable holds one value at a time. Assigning a new value replaces the old one.
  • If you need to remember several values at once, give each one its own variable.
  • A program that runs without errors can still be wrong. That’s called a logic error.
  • To debug, print your variables at each step and compare them with what you expect.
  • Choose clear variable names such as number1, number2 and number3, so it’s obvious they are different values.

✦ 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 · variables · debugging · input · arithmetic