Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialShayan Salehi
1,033 PointsNumber game - SyntaxError: 'break' outside loop
import random
generate random number 1 and 10
secret_num = random.randint(1, 10)
while True:
number guess from the player
guess = int(input("Guess the number between one and ten: "))
compare guess to secret number
if guess == secret_num: print("my number was indeed {}".format(secret_num)) break
else:
print("Wrong :(")
print hit or miss
When I run the script I get the following error:
treehouse:~/workspace$ python numgame.py
File "numgame.py", line 14
break
^
SyntaxError: 'break' outside loop
1 Answer
Christian Mangeng
15,970 PointsHi Shayan,
it seems to be a problem of the correct spacing. With the "break" command you break out of the "while" loop, so break has to be inside the while loop. This should work:
import random
secret_num = random.randint(1, 10)
while True:
guess = int(input("Guess the number between one and ten: "))
if guess == secret_num:
print("my number was indeed {}".format(secret_num))
break
else:
print("Wrong")
Shayan Salehi
1,033 PointsShayan Salehi
1,033 PointsThank you !! :)