Editor settings

General settings

Save settings

Learn to program with Python

Missing colon #

The colon : is an essential part of the syntax for defining code blocks, such as in if statements, for loops, function definitions (def), and more. Forgetting to include a colon after such statements can lead to a common syntax error. Let's explore how to recognize and resolve missing colon errors.

When you forget to include a colon after a statement that requires one, Python raises an error SyntaxError with the message expected ':'. With older versions, Python raises the error with the message invalid syntax instead.

To identify a missing colon error, carefully review the line of code where the error occurs. Look for statements like if, for, while, def, and class that are followed by a code block, which requires a colon.

Here's an example of incorrect code:

if x > 10 # Missing colon
    print("x is greater than 10")

And this is the output with the error message you would see:

File "main.py", line 1
    if x > 10 # Missing colon
              ^^^^^^^^^^^^^^^
SyntaxError: expected ':'

Exercise #

  • Add colons in the code so that the program runs without errors.

Tests #

  • can_you_vote(18) should print the message "You are eligible to vote."
  • can_you_vote(15) should print the message "You are not eligible to vote."
Output will be displayed here