信用卡号验证器无法正常工作 [英] Credit card number validator doesn't work correctly

查看:130
本文介绍了信用卡号验证器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def checksum(card_without_check):
    card_without_check = card_without_check[-1::-1]
    def numbers(string):
        return [int(x) for x in string]
    print(card_without_check)
    odd_numbers = numbers(card_without_check[0::2])
    even_numbers = numbers(card_without_check[1::2])

    odd_numbers = [x * 2 for x in odd_numbers]
    odd_numbers = [x - 9 if x > 9 else x for x in odd_numbers]
    print(even_numbers)
    print(odd_numbers)
    return sum(odd_numbers) + sum(even_numbers)

def check(checksum, check):
    return checksum % 10 == int(check)

card_number = input("Enter card number:\n")
print(checksum(card_number[:-1]))
print("Card is", check(checksum(card_number[:-1]), card_number[-1]))

该算法似乎适用于 4556737586899855之类的示例,但不适用于 30569309025904之类的示例。我已经遵循了这个过程,但是在处理数字方面却找不到缺陷,我可能只是在这里遗漏了一些难题。

This algorithm appears to work on examples like "4556737586899855", but not on examples like "30569309025904". I've followed the process and can't find flaws in how it's handling the numbers, I'm probably just missing some piece of the puzzle here.

遵循大纲此处,并使用了示例此处

I'm following the outline here and have used examples here.

推荐答案

我将这种解决方案用于基于Luhn公式的codeeval问题:

I used this solution for a codeeval problem based on Luhn's formula:

def checksum(n):
    nums = reversed(list(map(int, n)))
    doubled = (ele * 2 if ind % 2 else ele for ind, ele in enumerate(nums))
    return not sum(sum(map(int, str(ele))) for ele in doubled) % 10

问题说明中列出了这些步骤:

The steps are listed in the problem description:


从最右边的数字(即校验数字)向左移动,将每个数字的值加倍s位数如果此加倍运算的乘积大于9(例如7×2 = 14),则将乘积的数字相加(例如12:1 + 2 = 3、14:1 + 4 = 5)。
取所有数字的总和。
如果总模10等于0(如果总模为零),则根据Luhn公式,该数字有效;否则,该数字有效。否则无效。

From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (for example, 7×2=14), then sum the digits of the products (for example, 12:1+2=3, 14:1+4=5). Take the sum of all the digits. If the total modulo 10 is equal to 0 (if the total ends in zero) then, according to the Luhn formula, the number is valid; otherwise, it is not valid.

这篇关于信用卡号验证器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆