2017-04-04 13 views
-2

2番目のbreakコマンドでこのエラーが発生します。なぜこうなった? それは言う:'Break' Outside Loop - Python - Number Guess Game

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ループ