为什么即使满足条件,此代码的最后一行也不起作用?这是来自"自动化无聊的东西"一书


import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")
for guesses_taken in range(1, 7):
guess = int(input("Take a guess"))
if guess < secret_number:
print("This number is too low")
elif guess > secret_number:
print("This number is too high")
else:
break
if guesses_taken == 6:
print(f"Nope, the number I was thinking about was {secret_number}")

#this line won't display even when the number is guessed correctly
if guess == secret_number and guesses_taken < 6:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")

原始代码有一个缩进错误,我通过将最后四行代码移到循环区域来修复这个错误。遗憾的是,即使猜对了数字,最后一行仍然不会显示。

那是因为你缩进错了。break下面的内容应该是无缩进的,这样它就不是for循环的一部分:

import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")
for guesses_taken in range(1, 7):
guess = int(input("Take a guess"))
if guess < secret_number:
print("This number is too low")
elif guess > secret_number:
print("This number is too high")
else:
break
if guesses_taken == 6:
print(f"Nope, the number I was thinking about was {secret_number}")

if guess == secret_number and guesses_taken < 6:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")

然而,我想补充的是,即使这个版本的代码有一个错误:如果用户在最后一次尝试中猜对了,输出仍然是Nope, the number I was thinking about was {secret_number}

首先你并不需要

if guesses_taken == 6:

问题可能是break语句和

在同一个缩进层上
if guess == secret_number and guesses_taken < 6:

这是你的代码,应该工作得很好。

import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")
for guesses_taken in range(1, 7):
guess = int(input("Take a guess"))
if guess == secret_number:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")
break
elif guess < secret_number:
print("This number is too low")
else:
print("This number is too high")
else:
print(f"Nope, the number I was thinking about was {secret_number}")

你真的不需要<6检查,因为这将发生当你超过你的循环范围,从1到7

<r;删除第12-13行>

第8行到第13行:

if guess < secret_number:
print("This number is too low")
elif guess > secret_number:
print("This number is too high")
else:
break

从逻辑上考虑:

  • 如果猜测小于所选的数字,通知用户他们的猜测太低
  • 如果猜测值大于所选数字,则通知用户他们的猜测值过高
  • 否则,跳出循环(本质上是结束代码)

第三个条件满足的唯一情况是,数字既不太高,也不太低。只有当数字等于所选的数字时,这个数字既不太高也不太低。else语句是导致问题的原因。删除它。

试试下面的代码,看看是否能解决您的问题:

import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")
for guesses_taken in range(1, 7):
guess = int(input("Take a guess"))
if guess < secret_number:
print("This number is too low")
elif guess > secret_number:
print("This number is too high")
elif guess == secret_number:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")
else:
break
if guesses_taken == 6:
print(f"Nope, the number I was thinking about was {secret_number}")

#this line won't display even when the number is guessed correctly
if guess == secret_number and guesses_taken < 6:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")

相关内容

  • 没有找到相关文章

最新更新