-2
credit = "1234 2347 1845 9023"
credit_card = credit.replace('-','').replace(' ','')
a = len(credit_card)
sum_1 = 0
sum_2 = 0
sum = 0

list_even = []
for i in range (a,0,-1):
    if i%2 != 0:
        sum = sum + int(credit_card[i])
    else:
        list_even.append(int(credit_card[i-1]))


list_even_double = list(map(lambda x: x*2,list_even))

for j in range(len(list_even)):
    if list_even_double[j]>=10:
        sum_2 = sum_2 + list_even[j][0]+ list_even[j][1]
    else:
        sum_1 = sum_1+list_even_double[j]

total = sum_2+sum_1

if total%10 == 0:
    print("Valid")
else:
    print("Not valid")

I am expecting to take odd digit number from end and take the total of it. After that take remaining even digit number and double each of the even digit number. If after doubling there is any 2digit number(23) I want it to take it as (2+3=5).And sum it with the other. after that add those total odd and even if it gets divisible by 10 then its valid or else its not

Chris
  • 26,361
  • 5
  • 21
  • 42
  • 4
    You didn't mention what errors you get. Also, don't use `sum` as name, it's a built-in – buran Apr 24 '23 at 17:00
  • For one, your `if i%2 != 0` logic flow captures the credit card digit when `i` is not an even number, which I think is correct given your logic, BUT... your `else:` triggers when it IS even, but collects `i-1`, which would be the digit of the credit card that you will capture as 'odd' in your next iteration. Basically your `list_even` list ends up with the same digits that you process in your `sum=sum + int(credit_card[i])` logic. I think you mean to do `list_even.append(int(credit_card[i]))` instead. – JNevill Apr 24 '23 at 17:12
  • Why are yo uappending `credit_card[i-1])`? `credit_card[i]` is the even digit, you're appending the digit before it, which is the odd digit. – Barmar Apr 24 '23 at 17:12
  • I believe `sum_2 = sum_2 + list_even[j][0]+ list_even[j][1]` should be referring instead to `list_even_double` as `list_even` is a list of single digit integers. – JNevill Apr 24 '23 at 17:16
  • 1
    I get the sense that even the most rudimentary troubleshooting steps haven't been taken here. – JNevill Apr 24 '23 at 17:17
  • `list_even_double[j]` would need to be converted to a string in order to access `[0]` too. Then because it's involved in a sum, it would need to be converted back to a number. `int(str(list_even_double[j])[0])` would be more appropriate. `total=sum_2+sum_1` isn't taking into account the `sum` from your `odd` digits either. – JNevill Apr 24 '23 at 17:22

1 Answers1

0

Your implementation of the Luhn algorithm is more cumbersome than it needs to be. Given a string of digits (of arbitrary length), all you need is this:

from itertools import cycle

LMAP = {
        '0': 0,
        '1': 2,
        '2': 4,
        '3': 6,
        '4': 8,
        '5': 1,
        '6': 3,
        '7': 5,
        '8': 7,
        '9': 9
    }

NMAP = {
        '0': 0,
        '1': 1,
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 7,
        '8': 8,
        '9': 9
    }

def isvalid(n):
    c = cycle((NMAP, LMAP))
    _sum = 0
    for d in n[::-1]:
        _sum += next(c)[d]
    return _sum % 10 == 0
    
print(isvalid('5234123412341239'))
print(isvalid('5234123512341239'))

Output:

True
False
DarkKnight
  • 19,739
  • 3
  • 6
  • 22