我在做作业,我有点不知所措。我的任务之一是,我需要一个If语句来检查输入的数字是否为16个字符长,这是我目前为止的代码:
#the input
CreditCardNum = input("Input a credit card number(no spaces/hyphens): ")
#The if statements
if str(CreditCardNum) != len(16):
print("This is not a valid number, make sure the number is 16 characters.")
elif str(CreditCardNum) == len(16):
if str(CreditCardNum[0:]) == 4:
print("The Card is a Visa")
elif str(CreditCardNum[0:]) == 5:
print("The Card is a Master Card")
elif str(CreditCardNum[0:]) == 6:
print("The Card is a Discover Card.")
else:
print("The brand could not be determined.")
Python没有开关函数,所以可以使用}。在
if elif
或{你的案子绝对是字典式的。在
这就是我相信你在寻找的逻辑。在
如果卡片长度是16,它会检查第一个字符以确定哪种类型。在
说明
n = len(CreditCardNum)
在变量n
中存储输入字符串中的字符数。同样,输入的第一个字符。在len(16)
没有逻辑意义。您要将n
与另一个整数进行比较。在mystr[0]
。在你可以试试这样的方法:
我不知道您试图在外部
elif
内的条件语句中做什么,但我假设您正在尝试获取CreditCardNum
的第一个字符?在相关问题 更多 >
编程相关推荐