'打破'外循环 - 蟒蛇 - 数字猜猜游戏

时间:2017-04-04 22:58:45

标签: python python-3.4

我继续为第二个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”是错误的意思。 有人可以解释一下吗?

1 个答案:

答案 0 :(得分:1)

阅读错误说明:

for (std::size_t i = 0; i < course.size(); ++i) {
  std::cout << course[i] << std::endl; // or '\n' if you don't need flushing
}

确保缩进整段代码,使其成为for (std::size_t i = 0; i < course.size(); i++) { for (std::size_t j = 0; j < course[i].size(); j++) { std::cout << course[i][j]; } std::cout << '\n'; } 循环的一部分