2017-04-04 109 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循環的一部分