拆分字符或int::Python 3

2024-06-29 01:07:13 发布

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

我正在做一个自定进度的课程,EDX平台上的CS50,目标是要求用户提供信用卡号码,然后验证它是否是有效的美国运通卡、万事达卡或Visa卡。你知道吗

我一直在尝试拆分一个大于10的数字(格式化为字符串),结果是例如“14”=“1”和“4”。这两个数字都将附加到一个列表[]。你知道吗

我试图实现一个信用卡校验和检查的算法发明的基础上汉斯彼得卢恩,我被困在算法的第二部分(写为代码的注释),因为作为算法指定我必须分裂成14分为14,然后做总和。你知道吗

测试信用卡:378224631005 到目前为止,我得到了这个结果: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()

Tags: theto算法ifismaindef信用卡