Hi, I'm working through some youtube tutorials and doing exercises I've found on the web to try and solidify the knowledge.
One of the exercises was to create a number guessing game, which was simple enough using a while loop, but when I tried to add extra if statements to the while loop it went a bit funny. Here's what I came up with:
It works as intended the first time, but after the first incorrect guess (if correct the loop breaks and you can close as intended), it just prints the "too high" result regardless of whether the guess was too low, correct, or too high. So, I'm a bit stumped.
I originally had them all as "if" statements not "elif", but it's the same result. Why does the loop always print the same answer after the first guess instead of starting over like I'd expected it to?
One of the exercises was to create a number guessing game, which was simple enough using a while loop, but when I tried to add extra if statements to the while loop it went a bit funny. Here's what I came up with:
Code:
answer=int(raw_input("I'm thinking of a number 1 and 50, have a guess!"))
while answer != 32:
if answer>=25 and answer<35:
print "You're getting very warm now!"
elif answer<25 and answer>10:
print "Too low"
elif answer<= 10:
print "You couldn't be further off"
elif answer>= 35:
print "Too high that time!"
print " Guess again!"
answer=raw_input("Guess a number between 1 and 50: ")
else: print "Well done!"
raw_input("Hit enter to close window")
It works as intended the first time, but after the first incorrect guess (if correct the loop breaks and you can close as intended), it just prints the "too high" result regardless of whether the guess was too low, correct, or too high. So, I'm a bit stumped.
I originally had them all as "if" statements not "elif", but it's the same result. Why does the loop always print the same answer after the first guess instead of starting over like I'd expected it to?
Last edited: