← All coding practice

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

Python Coding Exercise #29: Fix the Syntax

Part of the series Python Practice Exercise — part 29

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

This is the first exercise I’m posting from Part 2 of the course. It’s different from the earlier ones: instead of writing a program from scratch, you’re given a broken one and have to find everything wrong with it.

The question

The following program contains several syntactic errors. Please fix the program so that the syntax is in order and the program works as specified by the examples below.

  number = input("Please type in a number: ")
  if number>100
    print("The number was greater than one hundred")
    number - 100
    print("Now its value has decreased by one hundred)
     print("Its value is now"+ number)
 print(number + " must be my lucky number!")
 print("Have a nice day!)
Please type in a number: 13
13 must be my lucky number!
Have a nice day!
Please type in a number: 101
The number was greater than one hundred
Now its value has decreased by one hundred
Its value is now 1
1 must be my lucky number!
Have a nice day!

My Solution

number = int(input("Please type in a number: "))
if number>100:
    print("The number was greater than one hundred")
    number -= 100
    print("Now its value has decreased by one hundred")
    print(f"Its value is now {number}")
print(f"{number} must be my lucky number!")
print("Have a nice day!")

The ten problems

There are ten separate problems in the broken program. Some are syntax errors, which stop Python from even starting the program. Others only appear when the line runs, and one gives no error at all. Here they are, in the order they appear:

#ProblemWhat Python saysThe fix
1The first lines are indented by two spaces, but nothing came before themIndentationError: unexpected indentStart top-level lines at the left edge
2if number>100 has no colon at the endSyntaxError: expected ':'if number>100:
3number comes from input(), so it’s a string, and a string can’t be compared with 100TypeError: '>' not supported between instances of 'str' and 'int'int(input(...))
4number - 100 calculates the answer and then throws it awayno error at allnumber -= 100
5The text in print("Now its value ... one hundred) has no closing quoteSyntaxError: unterminated string literaladd the closing "
6print("Its value is now"+ number) is indented by 5 spaces, not 4IndentationError: unexpected indentindent by exactly 4 spaces
7"Its value is now" + number joins text and a number, and has no spaceTypeError: can only concatenate str (not "int") to strf"Its value is now {number}"
8The last two print lines are indented by one spaceIndentationError: unindent does not match any outer indentation levelstart them at the left edge
9number + " must be..." adds a number and textTypeError: unsupported operand type(s) for +: 'int' and 'str'f"{number} must be..."
10The last print("Have a nice day!) has no closing quoteSyntaxError: unterminated string literaladd the closing "

You can see every one of these messages by running the pieces separately. The messages above are copied exactly from Python’s output. (The wording of some messages differs slightly between Python versions.)

Python reports one error at a time

If you run the broken program as it is, Python shows only the first problem it finds:

  File "broken.py", line 1
    number = input("Please type in a number: ")
IndentationError: unexpected indent

It doesn’t list all ten at once. So the practical approach is to fix one error, run the program again, fix the next one, and so on. Each fix reveals the next.

Three kinds of errors

Notice that the problems fall into three groups:

  • Syntax and indentation errors (1, 2, 5, 6, 8, 10). Python spots these before running anything. The program refuses to start.
  • Runtime errors (3, 7, 9). The syntax is fine, so the program starts, but it crashes when it reaches that line, because the types don’t fit together: a string against a number.
  • A logic error (4). number - 100 is perfectly valid Python. It just doesn’t do anything useful, because the result isn’t stored. There’s no error message, so the only way to find it is to notice the wrong output: the value never decreased.

How My Solution Works

Here is the fixed program, step by step:

1. Read the number as an integer. int(input(...)) converts the typed text to a whole number, so it can be compared and used in arithmetic. (See Exercise #08.)

2. The if. if number>100: is true only when the number is greater than 100, and the colon at the end is required. The four lines indented under it run only in that case.

3. Subtract 100 and store it. number -= 100 is the shorthand for number = number - 100. It stores the smaller value back in number. (The -= shortcut works like the *= in Exercise #18.)

4. Print with f-strings. f"Its value is {number}" puts the number straight into the text, with no conversion needed and a space exactly where you type it.

5. The last two lines are not indented. They start at the left edge, so they are outside the if and run for every number. That’s why both samples end with the lucky number line and Have a nice day!.

Here is how the two examples go through the program:

InputIs it over 100?Lines printed
13no, the if is skipped13 must be my lucky number! and Have a nice day!
101yes, the if runs, and number becomes 1the four if lines (with value 1), then 1 must be my lucky number! and Have a nice day!

For exactly 100, the if is also skipped, because 100 is not greater than 100. It prints 100 must be my lucky number!.

University Solution

The University Solution fixes the same problems, but joins text and numbers with + and str(), instead of using f-strings:

number = int(input("Please type in a number: "))
if number > 100:
    print("The number was greater than one hundred")
    number = number - 100
    print("Now its value has decreased by one hundred")
    print("Its value is now " + str(number))
print(str(number) + " must be my lucky number!")
print("Have a nice day!")

Two differences:

  • number = number - 100 does the same thing as number -= 100, written out in full.
  • str(number) converts the number to text, so that + can join two pieces of text. Without it, you’d get the TypeError from problems 7 and 9.

With +, nothing adds a space for you, so the space has to be inside the quotes: "Its value is now " + str(number). If you forget it, the output is Its value is now1. This is one reason f-strings are often easier: f"Its value is now {number}" shows the finished sentence in one piece. Both versions give the same output.

Tips for fixing broken code

  • Read the message and the line number. Python tells you what kind of error it is and where it noticed it. The real mistake is sometimes on the line just before.
  • Fix one error at a time, then run the program again.
  • Use four spaces for each level of indentation, and use the same amount throughout. Mixing two, four and five spaces is what caused three of the problems here.
  • Count your quotation marks and brackets. Every opening one needs a closing one. A good code editor highlights the whole string, so a missing quote makes the colours go wrong.
  • Check types when you see TypeError. Ask whether each value is text or a number, and whether it needs int(), str() or an f-string.
  • If a value doesn’t change, look for a missing assignment. number - 100 calculates, but only number = number - 100 or number -= 100 stores.

Key takeaways

  • A syntax error stops the program before it starts. Python reports the first one it finds, so fix them one at a time.
  • A TypeError appears when a line runs with the wrong types, such as comparing text with a number or adding a number to text.
  • Every if line must end with a colon, and the lines under it must be indented by the same amount.
  • Every opening quotation mark needs a closing one.
  • input() returns text. Convert it with int() before doing maths.
  • number - 100 doesn’t change number. Store the result with number = number - 100 or number -= 100.
  • Lines at the left edge, outside the if, run in every case.

✦ 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 · syntax · debugging · indentation · errors