gpt4 book ai didi

python - 我如何打印这个 elif 字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:11 24 4
gpt4 key购买 nike

我正在尝试创建一个猜谜游戏,并且运行良好。但是,我想包括一个部分,如果用户输入的数字大于 100,系统会告诉您您的选择应该小于 100。下面的代码似乎没有这样做。我究竟做错了什么?

import random
comGuess = random.randint(0,100)
while True:
userGuess = int(input("Enter your guess :"))
if userGuess > comGuess:
print ("Please go lower")
elif userGuess < comGuess:
print ("Please go higher")
elif userGuess > (100):
print ("Your choice should be less than 100")
elif userGuess <1:
print ("Your choice should be less than 100")
else:
print ("Great, you got it right")
break

最佳答案

任何大于100的数字肯定会高于目标,进入if条件。任何小于1的数肯定会低于target,进入第一个elif。如果您想验证用户的输入,您应该在之前将其与comGuess 进行比较:

if userGuess > (100):
print ("Your choice should be less than 100")
elif userGuess <1:
print ("Your choice should be less than 100")
elif userGuess > comGuess:
print ("Please go lower")
elif userGuess < comGuess:
print ("Please go higher")
else:
print ("Great, you got it right")
break

关于python - 我如何打印这个 elif 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32728158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com