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!
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
Python Web Development Techdegree Student 8,257 PointsPython 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))
break
elif guess < secret_num:
print("My number is higher than {}".format(guess))
else:
print("My number is lower than {}".format(guess))
cguesses.append(cguess)
else:
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':
game()
else:
print("Bye!")
game()
[MOD: added ```python formatting -cf]
1 Answer

Chris Freeman
Treehouse Moderator 68,390 PointsThe 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.