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

2024-10-02 02:25:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用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") `

谢谢


Tags: theto程序内容inputifmachinecard
3条回答

您正在将“输入卡”与列表进行比较。尝试:

if inputCard.lower() == "no":

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

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

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

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

if inputCard.lower() == 'no'

这种方式将接受noNoNOnO

inputCard是str,["NO","no"]是list。它们将不相等。您可以这样尝试

if inputCard.lower() == 'no':

或者

if inputCard.upper() == 'NO':

相关问题 更多 >

    热门问题