提问人:deafening_radio 提问时间:8/15/2023 最后编辑:Silvio Mayolodeafening_radio 更新时间:10/1/2023 访问量:28
为什么我的暖/冷 elif 语句直到程序结束才打印出来?
Why won't my warm/cold elif statements print until the end of program?
问:
我试图让输入后出现较暖/较冷的文本,但直到程序结束才打印?
我认为这与elif声明有关吗?我一直与其他项目中的那些人有麻烦(因此不得不将刽子手放在次要位置。我正在尝试制作一个程序,您可以在其中猜测 1-50 之间的数字,但我不知道如何纠正这个问题?
import random
print("Welcome to the number guessing game!")
print("")
guesses_allowed = 10
number = random.randint(1, 50)
guess = ""
for i in range(guesses_allowed):
guess_number = int(input("Please guess a number between 1 and 50: "))
guess = int(guess_number)
if guess == number:
print("Congrats you guessed correctly")
elif guess < number:
print("number is higher.")
else:
print("number is lower.")
if (i == guesses_allowed - 1):
print("Sorry, have run out of guesses")
if abs(guess - number) <= 10:
print("You're warm!")
elif abs(guess - number) <= 20:
print("You're getting warmer.")
elif abs(guess - number) <= 30:
print("You're cold")
else:
print("You're freezing")
答:
0赞
Jelle
8/15/2023
#1
如果将第 14 行缩进到一个选项卡的末尾,则该代码也将位于 for 循环的正文中。这意味着每一轮都会检查约束,而不是只在最后检查,并且游戏可以正常运行。
评论
if guess == number