嘿伙计们这里有蟒蛇noobie。我试图确定用户使用哪种类型的信用卡以及信用卡是否有效。对于以下示例,Visa卡以4号开头,两张卡都有效,因为它们都以4开头。如果前面有零,则跳过它们。是否有内置函数来绕过一个数字或者我必须输入每种情况?感谢您的时间。
示例:
#VISA 0004222222222222 valid
#VISA 4111111111111111 valid
def计算(creditNumber): length = len(creditNumber) oddSum = 0 evenSum = 0
if ( length == 0 ):
return 0
else:
if length % 2 == 0:
last_number = int( creditNumber[-1])
evenSum = evenSum + last_number
return evenSum + calculation( creditNumber[:-1] )
else:
last_number = int( creditNumber[-1] )
last_number = 2 * last_number
addSum = last_number // 10 + last_number % 10
oddSum = oddSum + addSum
return oddSum + calculation( creditNumber[:-1] )
def luhnsCheck():
creditNumber = input ( "What is your credit card number?" )
#Check to see which type of credit card the user has
# American Express starts with 34 or 37
if creditNumber[0-15]
# Discover starts with 6011
# MasterCard starts with 51 or 52 or 53 or 54 or 55
# VISA starts with 4
creditcard_number = calculation( creditNumber )
# Valid Card
if creditcard_number % 10 == 0:
print( "Valid card" )
# Invalid Card
else:
print( "We do not accept that kind of card" )
luhnsCheck()
答案 0 :(得分:-1)
您可以复制当时的号码并将该号码放入列表中。然后,如果第一个数字为零,则将其从列表中删除。重复此操作直到前面没有零。