Your own while / else: Invalid Syntax


#1



https://www.codecademy.com/en/courses/python-beginner-en-cxMGf/0/8?curriculum_id=4f89dab3d788890003000096


File "python", line 8
print guess
^
SyntaxError: invalid syntax


Do not know how this is invalid. Would appreciate explanation


from random import randint
 
random_number = randint(1, 10)
guesses_left = 3

while guesses_left > 0:
    guess = int(raw_input("Your guess: ")
    print guess
    if guess == random_number:
        print "You win!"
        break
else:
    print "Sorry, you lose!"
    break
guess_left -= 1


#2

look at the line above, in particular the parentheses


#3

Right, thank you, solved that.

Have a different problem now

from random import randint

random_number = randint(1, 10)
guesses_left = 3

while guesses_left > 0:
guess = int(raw_input("Your guess: "))
print guess
if guess == random_number:
print "You win!"
break
else:
print "Sorry, you lose!"
guesses_left -= 1

When I run this code, the code continues infinitely instead of stopping at three tries. What am I missing?


#4

you never reduces guesses_left inside the loop, so the loop condition never becomes false


#5

Problem solved, thank you


#6

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