8/19 If statement returns syntax errors


#1

My code matches what several people have said should work, but it's returning a syntax error. The code looks like this:

# 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."

The error is:

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

SyntaxError: invalid syntax

I even deleted the colon at the end of the if statement, and it throws another syntax error.

File "python", line 11
print "You win!"
^
SyntaxError: invalid syntax

What am I missing?


#2

Ah, I got it. I am missing the second ) at the end of line 9.


#3

Check the indentation too.

while guesses_left > 0:
guess = int(raw_input("Your guess: ")  <---
    if guess == random_number:
    .
    .
    .

I'm not 100% on python (yet), but I think you create an empty while loop there. Best case scenario it ain't pretty at least :wink:


#4

I'm new to programming entirely, so I'm all for any feedback! I didn't run into any issues with an empty loop, but I'd love to know what had you thinking that I may. I'd like to avoid making them. :smile:


#5

while condition:
    code_inside
code_outside

Is how a while loop should be formatted. Always paying attention to indentation is a key to writing python code that won't end up making you tear your hair out.


#6

Yep. I had the exact same error! Funny how one little parenthesis can ruin your entire program :wink: