我一直在设置无效的语法错误
forguessesTaken in range(1, 7):
行。 Python一直说:
是无效的语法。这真让我抓狂。有人可以帮忙吗?
# this is a guess the number game.
import random
secretnumber = random.randint(1,20)
print('I am thinking of a number between 1 and 20.')
# ask the player to guess 6 times.
forguessesTaken in range(1, 7):
print('take a guess.')
guess = int(input())
if guess < secretnumber:
print('your guess is too low.')
elif guess > secretnumber:
print('your guess is too high.')
else:
break # this condition is the corret guess!
if guess == secret number:
print('good job! You guessed my number in ' + str(guessestaken) + 'guesses!')
else:
print('nope. the number i was thinking of was ' + str(secretnumber))
答案 0 :(得分:1)
python中没有名为forguessesTaken
的关键字。也许你忘记在for
和guessesTaken
之间留出空格。在它们之间留一个空格:
for guessesTaken in range(1,7):
// remaining code...
答案 1 :(得分:0)
请改用此代码:
for guessesTaken in range(1, 8):
if guessesTaken == 8:
print('Nope. The number I was thinking of was', secretnumber)
break
guess = int(input('Take a guess: '))
if guess < secretnumber:
print('Your guess is too low.')
elif guess > secretnumber:
print('Your guess is too high.')
else:
print('Good job! You guessed my number in', guessesTaken, 'guesses!')
break