當我運行這個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)
看起來你的縮進關閉1平從'猜測=輸入()'到程序結尾。 – karthikr
你有一個'while guessesTaken <6'。 'print('猜一猜')在循環中。之後的一切都在循環之外,因爲你沒有縮進它。 – Ryan