Your own while / else


#1

Hi folks -
cant seem to shake this one - tried several combinations - still not dice.

from random import randint

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

while guesses_left > 0:
    guess = int(raw_input("Your guess: ")
    if guess == random_number:
        print "you win!", random_number
        guess_left -= 1
        break
else:
    print "you lose", random_number

I am probably missing the obvious, as usual. Please tell me.


#2

currently the guesses are only reduced if the user guesses the right number.

the guesses_lef -= 1 should be outside the if statement, but in the while loop


#3

from random import randint

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

while guesses_left > 0:
    guess_left -= 1
    guess = int(raw_input("Your guess: ")
    if guess == random_number:
        print "you win!", random_number
        break
else:
    print "you lose", random_number

tried doing so - didnt work - only got a 'syntaxerror'


#4

A syntax error means you're missing something structure-wise in the code. Look at what line it's reported at and check the stuff in that area, particularly in front of where it's reported. You can also temporarily remove or comment things out until it's gone as a means of finding it.


#5

this line:

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

the number of parentheses don't match

and this line:

guess_left -= 1

guess_left is not the right variable name, what is the variable where you stored the remaining guesses?


#6

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