Python Loops 8: Your own while / else


#1

Okay so i'm not really comprehending why this isn't working...

from random import randint

random_number = randint(1, 10)

guesses_used = 0
guesses_left = 3

while guesses_left >= 1 and guesses_left <= 3:
    guess = int(raw_input("Your guess: ")
    if guess == random_number:
        print 'You win!'
        break
    elif guess != random_number:
        guesses_used += 1
        guesses_left -= 1
        print 'Incorrect! You\'ve used %s guesses. You have %s guesses left!' (guesses_used, guesses_left)
    else:
        print 'You lose.'
        break

I get this error for some reason:

Please send some feedback my way, any help is appriciated!


#2

The above line is missing the format operator before the variables tuple:

... % ( var1, var2)

I don't see anythng wrong with the syntax of line 10. Add the above missing operator and test again. If you get the same error, we'll have to look a little harder (and test your code).


#3

Okay so first of all, I found the culprit of the syntax error! One little parenthesis at the end:

guess = int(raw_input("Your guess: ")**)**

But now I have a new problem after successfully implementing you piece of code:

I also added the int() before the variables

I'm just not understanding what is wrong honestly...


#4

The else: needs to line up with the while. Back it off 4 spaces.


#5

Resulting error:


#6

Oops. I should have actually read the error in depth :wink:

I fixed it simply by deleting the break under the else:

Thank you for you help though!


#7

Try removing the break from the else: statement.