'Break' 外循环 - Python - 猜数字游戏
'Break' Outside Loop - Python - Number Guess Game
我在第二次 break 命令时一直收到此错误,而不是第一次。为什么会这样?
它说:
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
第二个 "if not guess: break" 是关于错误的内容。
有人可以解释一下吗?
阅读错误描述:
>>> if True: break
SyntaxError: 'break' outside loop
确保缩进整段代码,使其成为 while
循环的一部分
我在第二次 break 命令时一直收到此错误,而不是第一次。为什么会这样? 它说:
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
第二个 "if not guess: break" 是关于错误的内容。 有人可以解释一下吗?
阅读错误描述:
>>> if True: break
SyntaxError: 'break' outside loop
确保缩进整段代码,使其成为 while
循环的一部分