gpt4 book ai didi

python - 为什么我的 if 语句命令不起作用

转载 作者:行者123 更新时间:2023-11-30 22:07:26 24 4
gpt4 key购买 nike

我正在尝试用Python中的这段代码对ATM机进行编程。但不管输入什么,都只是说卡输入成功。

inputCard = input("Welcome to the atm machine, please insert your credit card (Type 'Yes' when you have done so) ") 

if inputCard == ['No', 'no']: #checks if card has been entered
print ("Please retry")

else:
print ("Card is successfully inputed") `

谢谢

最佳答案

相等运算符== 比较输入(字符串)是否等于右侧(列表)。直观上,列表永远不会等于字符串。

因此,使用 in 运算符来查看答案是否在可能的选项中:

if inputCard in ('No', 'no'):

或者,将答案转换为小写,然后使用 ==:

if inputCard.lower() == 'no'

这种方式将接受noNoNOnO

关于python - 为什么我的 if 语句命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52460157/

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