Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Kimmo Ojala
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Python SyntaxError: invalid syntax

What is wrong with my code? I get a syntax error at row 19.

import random

def game():
    secret_num = 5
    cguess = random.randint(1, 10)
    cguesses = []

    while len(cguesses) < 5:
        if cguess == secret_num:
            print("You got it! My number was {}".format(secret_num))
        elif guess < secret_num:
            print("My number is higher than {}".format(guess))
            print("My number is lower than {}".format(guess))

        print("You didn't get it! My number was {}".format(secret_num))
    play_again = input("Do you want to play again? Y/n ")
    if play_again.lower() != 'n':


[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The line cguesses.append(cguess) closes the while loop code block. Then the else has no associated if, while, for, or try loop. This causes the syntax error.