Python Python Basics (2015) Number Game App Number Game Refinement

Shayan Salehi
Shayan Salehi
1,033 Points

Number 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
Christian Mangeng
15,967 Points

Hi 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")