gpt4 book ai didi

python - 如何在Python3中检查用户输入?

转载 作者:行者123 更新时间:2023-11-28 21:35:08 24 4
gpt4 key购买 nike

所以我的代码是这样说的:

a = input("Choose a letter")

if a == "A" or "B" or "D":
cr = "false"

elif a == "C":
cr = "true"

if cr == "false":
print("Sorry you did not asnwer correctly")
import sys
sys.exit()

elif cr == "true":
print("Well done!")

但出于某种原因,无论我输入什么,它都说它是错误的。有什么帮助吗?我是 python 新手,来自 Lua。

最佳答案

如果 a == "A"或 "B"或 "D": 其计算结果始终为 TRUE。因为“B”或“D”每次总是评估为TRUE。因此,无论如何,这可能归结为if 0 or 1 or 1

要解决此问题,您需要在每个条件运算符之后进行比较。如果a ==“A”或a ==“B”或a ==“D”:

关于python - 如何在Python3中检查用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52603048/

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