Can not hit winning condition. But code passed for next lesson


#1



I am not stuck per se. Codecademy allowed me to move on to the next exercise but I can not get my code to hit the winning condition even when my guess = the random number. If you have any advice it would be appreciated.


from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)
# print "Number is",random_number
guesses_left = 3
# Loop allows user to guess a random number three times.
while guesses_left > 0:
    # Retrieves guess with input verification loop
    guess = raw_input("Guess a number between 1 to 10: ")
    while 0 <= guess <= 10:
        guess = raw_input("Guess a number between 1 to 10: ")

    # Testing logic(when guess == random_number it is returning False)
    print "Random Number is:",random_number
    print "Your guess is:",guess
    test = guess == random_number
    print "Random Number == guess:",test
    # End of testing logic

    # Ends loop when random number is guessed.
    if guess == random_number:
        print "You win!"
        break
    # Prompts user to try again if there are guesses left.
    elif guesses_left > 1:
        print "Try Again"

    # Decrementation of Guesses left
    guesses_left -= 1
# Prompt that user loses when guesses_left == 0 and they did not guess the correct #.
else:
    print "You Lose"


#2

Problem solved. Made guess = int(guess).


#3

yes, well done. comparing a unicode (raw_input stores input as unicode) with a integer, is asking for trouble.

One more thing, this:

while 0 <= guess <= 10:

if you want to meet multiply conditions use the and keyword:

while 0 <= guess and guess <= 10

#4

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