Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Shayan 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,969 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 !! :)