Your own while/else loops

from random import randint

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

guesses_left = 3
# Start your game!
guess = int(raw_input("Your guess:"))
while guesses_left<=3:
   if guess == random_number:
     print "You win!"
  guesses_left -= 1
else :
  print "You lose."

my code is not working and i am unable to figure out why… help please!


how does the code behave differently then you expected? What error message did you get?


they are not showing any result when the number is not correct… i think there is problem with the else statement


if you want this behaviour for your program, you can add it? Not sure its part of exercises, but you are always free to expand programs you learn on codecademy


The indentation is invalid (so that code will not run at all) and you have an infinite loop (the condition to do another iteration is always met)


can you rectify the code please


Well, no, but running the code will tell you where the indentation issue is (though it may only be something that you added in this post, your own code might be different and not have that indentation problem), and I did mention an infinite loop so you could take another look at your loop and think about when and why it would stop


[quote=“cryptn, post:1, topic:267654”]
Hi max


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