-2

I'm trying to implement the Luhn algorithm in a program and I'm trying to modify list items if they are larger than 9. I will attach the code I'm trying to write. variable iin is defined outside of function.

def luhn_checker():
    account_number = random.randint(100000000, 999999999)
    check_sum = random.randint(0, 9)
    numbers = []
    card_number = f'{iin}{account_number}'

    for i in card_number:
        numbers.append(int(i))
        print(numbers)

    numbers[0] = numbers[0] * 2
    numbers[2] = numbers[2] * 2
    numbers[4] = numbers[4] * 2
    numbers[6] = numbers[6] * 2
    numbers[8] = numbers[8] * 2
    numbers[10] = numbers[10] * 2
    numbers[12] = numbers[12] * 2
    numbers[14] = numbers[14] * 2
    print(numbers)
    for i in numbers:
        if i > 9:
            numbers[i] = numbers[i] - 9
Angel Caro
  • 93
  • 8
  • 1
    Could you clarify your question to describe the issue you face? And is the goal here to learn how to write the algorithm or to have a working Luhn checksum calculator? (ie: can you use libraries here?) – kerasbaz Aug 20 '20 at 00:13
  • 1
    `for i in numbers:` should be `for i in range(len(numbers)):` – Barmar Aug 20 '20 at 00:14
  • I'm just trying to have a working checksum calculator and i'm open to using libraries. – Angel Caro Aug 20 '20 at 00:32

2 Answers2

0

There is an error in your for-loop. It should be as follows:

for i, x in enumerate(numbers):
    numbers[i] = x - 9 if x > 9 else x
metahexane
  • 119
  • 11
0

Thanks for the replies, ended up using a luhn library.

Angel Caro
  • 93
  • 8