import random
print('Hello. What is your name?')
name = input()
print('Well, ' + name + ', I am thinking of a number between 1 and 20')
secretNumber = random.randint(1, 20)
for guessesTaken in range(1,7):
print('Take a guess.')
try:
guess = int(input())
if guess < secretNumber:
print('Your guess is too low')
elif guess > secretNumber:
print('Your guess is too high')
else:
break
except ValueError:
print('You are only allowed to enter an integer value!')
if guess == secretNumber:
print('Good job ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))
print('You took ' + str(guessesTaken) + ' guesses.')
答案 0 :(得分:0)
您未正确输入问题中的代码。
以下是提示:复制并粘贴,然后将<code>
放在代码之前,并在代码之后放置</code>
。
您得到的错误表明您没有维护Python所需的正确缩进。检查所有行中的缩进,您将立即解决。
答案 1 :(得分:0)
正如您在问题的评论中添加的那样,错误的行在try
之后。 TRY / EXCEPT块也需要缩进。 https://docs.python.org/2.7/tutorial/errors.html
请仔细查看示例,因为您会看到try
下的所有行都必须缩进,except
行必须与try
和w3m -dump_source nytimes.com > nytimes.html
处于同一级别下面的东西再缩进了一次。