Your own while/else


#1

from random import randint
guesses_left=0
while guesses_left<3:
random_number = randint(1, 10)

guess=int(raw_input("Your Guess?"))
if guess == random_number:
    print "You win!"
    break
guesses_left+=1

else:
print "You lose."

Error: Oops, try again. Make sure to decrement guesses_left!

Someone please help me out!


#2

+= is will increase, not decrease. What do you think is the operator for decreasing?


#3

Aah, very silly on my part. I edited it top to bottom and came out with this. IT WORKED!

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
guesses_left-=1

else:
print "You lose."

print "Correct number:"+ str(random_number)


#4

something else, won't your place your random_number outside the while loop? by placing it inside the while loop, each iteration of the loop a new random number is generated, look:

while guesses_left>0:
    random_number = randint(1, 10)
    print random_number

a new random number is generated every time


#5

Thank you again, corrected my code.


#6

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