Your own while / else 8. What is wrong with my code?


#1


from random import randint
while guesses_left > 0:
guess = int(raw_input("Your guess: "))
if guess == random_number:
print 'You win!'
break
guesses_left -=1
else:
print 'You lose.'`

random_number = randint(1, 10)
guesses_left =3

# Start your game!
`
i get error:
Oops, try again. Did you allow the user 3 guesses, or did you incorrectly detect a correct guess?


#2

@l1palas,
IMHO

from random import randint
#
#You have to declare the =guesses_left=
guesses_left = 3
#
#You will have to declare the =random_number=
random_number = int(randint(1,10))
print random_number

while guesses_left > 0:
    guess = int(raw_input("Your guess: \n"))
    guesses_left -=1
    if guess == random_number:
        print 'You win!'
        guesses_left = 0
        break
    
    if guesses_left == 0:
        print 'You lose'
        break
    #End of WHILE loop was reached
    #end of program

#3

Thanks!:grinning::heart_eyes:


#4

It's asking whether I had added an else in the while loop? Help pls? :confounded:


#5

@cpiponides,
Have a read
http://stackoverflow.com/questions/2802726/putting-a-simple-if-then-statement-on-one-line


#6

Thank you :slight_smile:


#7

I copied and pasted yours on my code, but it gave me an INFINITE LOOP, or it might be a LAG or a HANG


#8

I used this:

from random import randint

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!"
break
guesses_left -= 1
else:
print "You lose."