8/14 never wins


#1

Game never seems to win, but my code still passes to the next lesson... any ideas???

My Code:

from random import randint

guesses_left = 3

while guesses_left > 0:
random_number = randint(1, 10)
print random_number
guess = int(raw_input("Your guess: "))
guess = raw_input

if guess == random_number:
    print 'You win!'

    break

elif guess != random_number:
    print 'You guessed incorrectly!'
    guesses_left -= 1

else:
print 'You Lose'


#2

I can't tell if the 'else' statement is at the same indentation as the 'if' and 'elif' statements, but it's not necessary there because the 'if' and 'elif' statements take in all possible answers. Also, the line guess = raw_input shouldn't be there; the line above it takes care of the raw_input. I think that might be preventing the win because the second raw_input line is reassigning guess to "".

Maybe try something like this:

from random import randint
guesses_left = 3
while guesses_left > 0:
    random_number = randint(1, 10)
    guess = int(raw_input("Your guess: "))
    if guess == random_number:
        print 'You win!'
        break
    if guess != random_number:
        print 'You guessed incorrectly!'
        guesses_left -= 1
if guess != random_number:
    print 'You Lose!'
print 'Game Over!'

That way 'You Lose!' only prints if all the guesses are used up (so the program exits the while loop) and then the program reevaluates if the guess was incorrect. I'm sure there's an even more elegant way to do this. Good luck!


#3

you were right my friend I just had to remove that silly guess == raw_output, and that was that! greetings from greece!!!


#4

I'm glad it worked. Greetings from America and happy coding! :grinning: