Syntax error at if


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/0/8?curriculum_id=4f89dab3d788890003000096#


File "python", line 10
if guess == random_number:
^
SyntaxError: invalid syntax


I expect the programm to work and ask for my input


from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
# Start your game!
while guesses_left > 0:
    guess = int(raw_input("Your guess: ")
    if guess == random_number:
        print("You win!")
        break
    guesses_left -= 1
else:
    print("You lose..")


#2

...
while guesses_left > 0:
    guess = int(raw_input("Your guess: ") #You forgot a ) here.
...

#3

Thanks! This was driving me crazy.. Why does python show the syntax error in a different line, that it is actually at?


#4

It's just saying that the if statement shouldn't be there because they think it's a part of your int as your didn't close your bracket properly.

Python see it as

...
int(raw_input("Your guess: ") if guess == random_number:
...

in a way. That's what I think. So it makes sense that the if would be a syntax error as that could've been ).


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.