2015-12-11 50 views
-1

當我運行這個python時,即使當我弄亂縮進時,也會返回一個錯誤,說'break在循環之外'。我也嘗試調整行間的差距,儘管這在python中無關緊要。請幫忙!Python'break'表示它在循環外部

# My guess the number game 
import random 

guessesTaken = 0 

print('Hello! What is your name?') 

userName = input() 
number = random.randint(1, 100) 

print('Well, ' + userName + ', I am thinking of a number between 1 and 100.') 

while guessesTaken < 6: 
    print('Take a guess.') 
guess = input() 
guess = int(guess) 
guessesTaken = guessesTaken + 1 

if guess < number: 
     print('Your guess is too low.') 

if guess > number: 
     print('Your guess is too high.') 

if guess == number: 
    break 

if guess == number: 
    guessesTaken = str(guessesTaken) 

print('Good job, ' + userName + '! You guessed my number in ' + guessesTaken + ' guesses!') 

if guess != number: 
    number = str(number) 
print('Nope. The number I was thinking of was ' + number) 
+0

看起來你的縮進關閉1平從'猜測=輸入()'到程序結尾。 – karthikr

+0

你有一個'while guessesTaken <6'。 'print('猜一猜')在循環中。之後的一切都在循環之外,因爲你沒有縮進它。 – Ryan

回答

1

在Python中,間距是決定代碼塊的一個因素。您的if語句與while具有相同的縮進,因此它們不在循環中。

另外,你可能想要pydoc input

所以您修復縮進和錯誤類型input報表後,它的工作原理:

jcomeau @嚮往:〜/ tmp目錄$蟒蛇test.py

Hello! What is your name? 
jc 
Well, jc, I am thinking of a number between 1 and 100. 
Take a guess. 
5 
Your guess is too low. 
Take a guess. 
66 
Your guess is too low. 
Take a guess. 
88 
Your guess is too high. 
Take a guess. 
77 
Your guess is too high. 
Take a guess. 
74 
Good job, jc! You guessed my number in 5 guesses! 

[如Robᵩ中指出,評論,如果它是Python3,input是正確的。]

+1

OP似乎在使用Python3,所以他使用'input'是正確的。回想一下,Python2'raw_input()'等價於Python3'input()'。 –

1

您的縮進不正確。請記住,在Python中,whileif的內容比while語句本身更縮進一級。

import random 

guessesTaken = 0 

print('Hello! What is your name?') 

userName = input() 
number = random.randint(1, 100) 

print('Well, ' + userName + ', I am thinking of a number between 1 and 100.') 

while guessesTaken < 6: 
    print('Take a guess.') 
    guess = input() 
    guess = int(guess) 
    guessesTaken = guessesTaken + 1 

    if guess < number: 
     print('Your guess is too low.') 

    if guess > number: 
     print('Your guess is too high.') 

    if guess == number: 
     break 


if guess == number: 
    guessesTaken = str(guessesTaken) 
    print('Good job, ' + userName + '! You guessed my number in ' + guessesTaken + ' guesses!') 

if guess != number: 
    number = str(number) 
    print('Nope. The number I was thinking of was ' + number) 
0
在Python縮進

是強制性的,因爲它你留下的循環應該是之前,你的代碼應該是這樣的:

# My guess the number game 
import random 

guessesTaken = 0 

print('Hello! What is your name?') 

userName = input() 
number = random.randint(1, 100) 

print('Well, ' + userName + ', I am thinking of a number between 1 and 100.') 

while guessesTaken < 6: 
    print('Take a guess.') 
    guess = input() 
    guess = int(guess) 
    guessesTaken = guessesTaken + 1 

    if guess < number: 
     print('Your guess is too low.') 

    if guess > number: 
     print('Your guess is too high.') 

    if guess == number: 
     break 

if guess == number: 
    guessesTaken = str(guessesTaken) 
    print('Good job, ' + userName + '! You guessed my number in ' + guessesTaken + ' guesses!') 

if guess != number: 
    number = str(number) 
    print('Nope. The number I was thinking of was ' + number) 
+0

你說得對,我會編輯它 – ddalu5