Your own while/else loops


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

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


#2

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


#3

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


#4

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


#5

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)


#6

can you rectify the code please


#7

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


#8

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


#9

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