如何解决卢恩算法

发布于 2025-01-23 16:13:47 字数 1377 浏览 1 评论 0原文

有很多有关如何撰写Luhn Algortim的信息。我也在尝试,我认为我很接近成功,但是我的代码中有一些错误,不知道在哪里。该测试卡是有效的卡,但我的算法则相反。你不知道为什么吗? 最终要帮助

test = "5573497266530355"
kazde_druhe = []
ostatni = []

for i in test:
    if int(i) % 2 == 0:
        double_digit = int(i) * 2

        if double_digit > 9:
            p = double_digit - 9
            kazde_druhe.append(p)
        else:
            kazde_druhe.append(double_digit)
    else:
        ostatni.append(int(i))

o = sum(ostatni)
k = sum(kazde_druhe)

total = o+k

if total % 10 == 0:
    print(f"Your card is valid ")
else:
    print(f"Your card is invalid ")

!谢谢大家的帮助。现在正在工作:-)

        test = "5573497266530355" kazde_druhe = [] ostatni = []
        
        for index, digit in enumerate(test):
            if index % 2 == 0:
                double_digit = int(digit) * 2
                print(double_digit)
        
                if double_digit > 9:
                    double_digit = double_digit - 9
                    kazde_druhe.append(double_digit)
                else:
                    kazde_druhe.append(double_digit)
            else:
                ostatni.append(int(digit))
    
     o = sum(ostatni)
    
     k = sum(kazde_druhe) 
    
    total = o+k if total % 10 == 0:
            print(f"Your card is valid ")
 else:
            print(f"Your card is invalid ")

there is a lot of information about how to write Luhn algortim. I'm trying it too and I think that I'am very close to succes but I have some mistake in my code and dont know where. The test card is VALID card but my algorithm says otherwise. Don't you know why? Thx for help

test = "5573497266530355"
kazde_druhe = []
ostatni = []

for i in test:
    if int(i) % 2 == 0:
        double_digit = int(i) * 2

        if double_digit > 9:
            p = double_digit - 9
            kazde_druhe.append(p)
        else:
            kazde_druhe.append(double_digit)
    else:
        ostatni.append(int(i))

o = sum(ostatni)
k = sum(kazde_druhe)

total = o+k

if total % 10 == 0:
    print(f"Your card is valid ")
else:
    print(f"Your card is invalid ")

Finally! Thank you all for your help. Now it is working :-)

        test = "5573497266530355" kazde_druhe = [] ostatni = []
        
        for index, digit in enumerate(test):
            if index % 2 == 0:
                double_digit = int(digit) * 2
                print(double_digit)
        
                if double_digit > 9:
                    double_digit = double_digit - 9
                    kazde_druhe.append(double_digit)
                else:
                    kazde_druhe.append(double_digit)
            else:
                ostatni.append(int(digit))
    
     o = sum(ostatni)
    
     k = sum(kazde_druhe) 
    
    total = o+k if total % 10 == 0:
            print(f"Your card is valid ")
 else:
            print(f"Your card is invalid ")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀お生予夺 2025-01-30 16:13:47

从此描述中

2。使用有效载荷,从最右边的数字开始。向左移动,将每个第二位数的值(包括最右边的数字)加倍。

您必须检查数字位置,而不是数字本身。

更改此:

for i in range(len(test)):
    if i % 2 == 0:

From this description

2. With the payload, start from the rightmost digit. Moving left, double the value of every second digit (including the rightmost digit).

You have to check the digit position, not the number itself.

Change to this:

for i in range(len(test)):
    if i % 2 == 0:
甜味超标? 2025-01-30 16:13:47

此代码有效。 :)

我尽可能地修复了您的代码。

test = "5573497266530355"
#test = "3379513561108795"

nums = []

for i in range(len(test)):
    if (i % 2) == 0:
        num = int(test[i]) * 2
        
        if num > 9:
            num -= 9

        nums.append(num)
    else:
        nums.append(int(test[i]))

print(nums)
print((sum(nums) % 10) == 0)

我发现您的代码出错的地方。

在线上:

for i in test:
    if int(i) % 2 == 0:

应该是:

for i in range(len(test)):
    if i % 2 == 0:

您不应该使用字符串的元素,您应该使用元素的索引。

This code works. :)

I fixed you code as much as i could.

test = "5573497266530355"
#test = "3379513561108795"

nums = []

for i in range(len(test)):
    if (i % 2) == 0:
        num = int(test[i]) * 2
        
        if num > 9:
            num -= 9

        nums.append(num)
    else:
        nums.append(int(test[i]))

print(nums)
print((sum(nums) % 10) == 0)

I found where your code went wrong.

On the line:

for i in test:
    if int(i) % 2 == 0:

It should be:

for i in range(len(test)):
    if i % 2 == 0:

You should not be using the element of the string you should be using the index of the element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文