好吧,所以我想我快到了,我的第一/第二部分在他们自己的情况下工作完美,但我无法将两者结合起来这是我到目前为止所拥有的,我认为错误在于最后一点,抱歉,我是 python 新手,所以我希望尽快掌握它
Edit3:我已经让它工作了(在你们的帮助下)但是现在当我输入 3782822463100050 时,它假设是无效的美国运通,但它显示为有效的美国运通......
Edit1:好的,例如,当我发布0378282246310005
(假美国运通)时,它说
Traceback (most recent call last):
File "C:/Users/Nat/Desktop/attempt.py", line 39, in <module>
print((cardType)+"Valid")
NameError: name 'cardType' is not defined
0378282246310005
但是当我像它一样插入一个随机数时
请输入您的信用卡号 0378282246310005
我们不接受那种卡
Edit2:最后你应该能够输入信用卡号码,它会说“你的“信用卡类型”是有效的(或无效的)
或者说“我们不支持该卡”
#GET number that will be tested
CreditNumber = input("Please enter your credit card number")
#SET total to 0
total=0
#LOOP backwards from the last digit to the first, one at a time
CreditCount = len(CreditNumber)
for i in range(0, CreditCount, -1):
LastDigit=CreditCard[i]
#IF the position of the current digit is even THEN DOUBLE the value of the current digit
if i % 2 ==0:
LastDigit=LastDigit*2
#IF the doubled value is more than 9 THEN SUM the digits of the doubled value
if LastDigit>9:
LastDigit=LastDigit/10+LastDigit%10
total=total + digit
#Figure out what credit card the user has
if ( CreditNumber[0:2]=="34" or CreditNumber[ 0:2 ] == "37"):
cardType = "Your American Express is"
elif ( CreditNumber[ 0 :4 ] =="6011"):
cardType = "Your Discover card is"
elif ( CreditNumber[0 :2 ] in [ "51", "52", "53", "54", "55"]):
cardType = "Your Mastercard is"
elif ( CreditNumber == "4" ):
cardType = "Your VISA card is"
else:
print( "We do not accept that kind of card")
if total % 10 == 0:
print((cardType)+"Valid")
else:
print((cardType)+"Invalid")