Implement the Luhn Algorithm - Implement the Luhn Algorithm

Tell us what’s happening:

I don’t understand why test 6 is incorrect although all the other tests are.

Your code so far

def verify_card_number(code: str) -> str:


    if code.isspace():
        return 'INVALID!'

    if '-' in code:
        split: list[str] = []
        split = code.split('-')
        code = ''
        for i in range(len(split)):

            code += split[i]

    if not code.isdigit():
        return 'INVALID!'

    code_separated: list[str] = []

    other_numbers: list[int] = []


    for i in range(0,len(code)):

        code_separated.append(code[i])

    for i in range(len(code_separated)):

        code_separated[i] = int(code_separated[i])
       
        other_numbers.append(code_separated[i])

    other_numbers.reverse()

    for i in range(1,len(other_numbers),2):

        other_numbers[i] = 2 * other_numbers[i]

    two_char_numbers = []
    single_char_numbers = []

    for i in range(0,len(other_numbers)):
        if other_numbers[i] > 9:

            two_char_numbers.append(other_numbers[i])

        if other_numbers[i] <= 9:
            single_char_numbers.append(other_numbers[i])

    for i in range(len(two_char_numbers)):
        two_char_numbers[i] -= 9

    final_numbers = []
    final_numbers.extend(two_char_numbers)
    final_numbers.extend(single_char_numbers)

    final_sum = sum(final_numbers)
    

    if final_sum % 10 == 0:
        return 'VALID!'

    else:
        return 'INVALID!'

    

print(verify_card_number('4111-1111-1111-1111'))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0

Challenge Information:

Implement the Luhn Algorithm - Implement the Luhn Algorithm

Github Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-luhn-algorithm/68507cadbf36aa089a84ce2d.md at main · freeCodeCamp/freeCodeCamp · GitHub

your function is failing to work when tested with 4539 1488 0343 6467, you may want to check with that one

you need to deal correctly with codes that include spaces or dashes

Is it supposed to output “VALID!” when tested with this?

yes, this is one of the codes for the failed test

Thank you very much, I solved the problem!