我正在EDX平台上进行自学课程CS50,目标是询问用户信用卡号,然后验证它是否是有效的美国运通卡,万事达卡或维萨卡。
我试图分裂一个大于10的数字(格式化为字符串),以便结果例如" 14" =" 1"和" 4"我们的想法是两个数字都会附加到列表[]。
我正在尝试根据Hans Peter Luhn发明的算法实施信用卡校验和检查,我被困在算法的第二部分(写作对代码的评论),因为算法指定我必须将14分成1 4然后做总和。
测试用信用卡:378282246310005 到目前为止我有这个结果:14 + 4 + 4 + 8 + 6 + 0 + 0 = 36 //差 我需要继续做的是:1 + 4 + 4 + 4 + 8 + 6 + 0 + 0 = 27 //好
这是我的代码:
def main():
cCNumber = str(input("Ingrese el numero de la tarjeta de credito: "))
if cCNumber[0] == "3" and (cCNumber[1] == "4" or cCNumber[1] == "7") :
numbers = listInts(cCNumber)
listSumador = checkSum(numbers)
listSumadorStr = str(listSumador)
print(listSumadorStr)
print("Amex")
#Convert string to a list of Ints.
def listInts(stRnumber):
listNumbers = []
for i in range(0,len(stRnumber),1):
listNumbers.append(int(stRnumber[i]))
return listNumbers
#Implement the following algorithm:
#1: Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.
#2: Add the sum to the sum of the digits that weren’t multiplied by 2.
#3: if the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!
def checkSum(numbers):
sumador = []
for i in range(1,len(numbers),2):
provisoryList = []
provisoryList.append(int(numbers[i]) * 2)
sumador.append(int(numbers[i]) * 2)
return sumador
if __name__ == "__main__":
main()