Python:信用卡验证码

Geo*_*ing -5 python validation

我的代码有问题.它运行良好但在某些情况下(例如输入= 8913266562747895)它给出了错误的答案.这是我的代码:

def ccv(s):
    k=list(s)
    if len(k)==19:
        for i in(0,1,2,3,5,6,7,8,10,11,12,13,15,16,17,18):
            if not k[i].isdigit():
                return False
        for i in(4,9,14):            
            if k[i]!=" " and k[i]!="-":
                return False
        l=[int(c) for c in s if c.isdigit()]
        if not 4<=l[0]and l[0]<=7:
            return False
        s=0
        for i in range(0,16, 2):
            l[i]=2*l[i]
            if l[i]/10!=0:
                l[i]= l[i]/10+(l[i]%10)       
        for i in range(16):
            s=s+l[i]
        return s%10==0
    elif len(k)==16:
        for i in range(16):
            if not k[i].isdigit():
                return False
        l=[int(c) for c in s if c.isdigit()]
        if not 4<=l[0]and l[0]<=7:
            return False
        else:          
            s=0
            for i in range(0,16, 2):
                    l[i]=2*l[i]
                    if l[i]/10!=0:
                        l[i]= l[i]/10+(l[i]%10)       
            for i in range(16):
                    s=s+l[i]
        return s%10==0
    else:
        return False
n=raw_input()
while n!="END" and n!="end":
    print ccv(n)
    n=raw_input()
Run Code Online (Sandbox Code Playgroud)

你能告诉我问题出在哪里吗?

And*_*zlo 7

我认为你正在寻找的是Luhn算法.它可以非常简洁地实现,如下所示:

def luhn(input):
    digits = [int(c) for c in input if c.isdigit()]
    checksum = digits.pop()
    digits.reverse()
    doubled = [2*d for d in digits[0::2]]
    total = sum(d-9 if d > 9 else d for d in doubled) + sum(digits[1::2])
    return (total * 9) % 10 == checksum
Run Code Online (Sandbox Code Playgroud)

Rosetta Code - Luhn算法的版本更短,但可能性较差.

PayPal已经公布了一份很好的伪造信用卡号码列表以供测试.

还要检查pycard,这是一个没有外部依赖关系的库.

  • @GeorgeDavidKing:"*以字符串*的形式获取信用卡号码" (2认同)