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

问题描述

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:

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

08-23 01:19