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

Python

Syntax Error

import random

def game():

    secret_num = random.randint(1, 10)

    guess = int(input("Guess a number: ")

    if secret_num == guess_num:
         print ("You got it! My number was {}.".format(secret_num))
    else:
         print("A miss!")


game()

I don't know why the syntax error is being shown.

3 Answers

guess = int(input("Guess a number: ")

count the number of ( then count the number of )

Thanks. But that's not the error. The error is being shown in the 'if' statement.

if secret_num == guess_num:
         print ("You got it! My number was {}.".format(secret_num))

In your code, guess_num was never defined. I think that is the error.

I put your code in a file and executed it:

  File "tmp.py", line 9
    if secret_num == guess_num:
                              ^
SyntaxError: invalid syntax

The reason you get this syntax error here is that the line before it you open a parenthesis but didn't close it. The python parser is looking for that closing parenthesis but finds a == instead, and errors there.

If you get an error on a line and you can't see anything wrong, try looking to see if it's confused about the line before.

Thanks. But I changed 'guess_num' to 'guess'. It still doesn't work.

if secret_num == guess:
         print ("You got it! My number was {}.".format(secret_num))