2017-04-04 106 views
-2

我不断收到第二个break命令的错误,而不是第一个。这是为什么发生? 它说:'Break'Outside Loop - Python - 猜数字游戏

SyntaxError: 'break' outside loop 

这里是我的代码:

import random 
import easygui 

secrets = random.randint(1, 200) 
secret = random.randint(1, 100) 
guess = 0 
tries = 0 

easygui.msgbox("""AHOY! I'm the Dreaded Pirate Roberts, and I have a secret! It is a number from 1 to 99. I'll give you 10 tries.""") 

while guess != secret and tries < 10: 
    guess = easygui.integerbox("What's yer guess, matey?") 
    if not guess: break 
    if guess < secret: 
     easygui.msgbox(str(guess) + " is too low, ye scurvy dog!") 
    elif guess > secret: 
     easygui.msgbox(str(guess) + " is too high, landlubber!") 
    tries = tries + 1 

if guess == secret: 
    easygui.msgbox("Avast! Ye got it! My secret is: **** ** ****! NO MATTER WHAT ****** SAYS, **** *****!!!!!!!!!!!!!!!!!") 

else: 
    easygui.msgbox("No more guesses! Better luck next time, matey!") 
    easygui.msgbox("Now, since ye lost, ye can try 1-50.") 

    while guess != secrets and tries < 15: 
     guess = easygui.integerbox("What's yer guess, matey?") 
     if not guess: break 
    if guess < secrets: 
     easygui.msgbox(str(guess) + " is too low, ye scurvy dog!") 

    elif guess > secrets: 
     easygui.msgbox(str(guess) + " is too high, landlubber!") 
    tries = tries + 1 

第二个 “如果不是猜测:破” 是错误的内容。 有人可以解释吗?

+0

解决您的压痕。 – user2357112

+2

什么错误?不要将代码作为图像发布,请复制粘贴实际文本(编辑问题时有一个代码块格式化选项)。 – Evert

+3

中断不在循环中。 – Shiping

回答

1

读取错误描述:

>>> if True: break 
SyntaxError: 'break' outside loop 

确保您缩进该代码的整个部分,使得它的while循环的一部分