Learn How to Work with Numbers and Strings by Implementing the Luhn Algorithm - Step 35

Tell us what’s happening:

which card_number shall I change? I changed the card N many times it didn’t work.
Simply I didn’t understand the project.

Your code so far


# User Editable Region

def verify_card_number(card_number):
    sum_of_odd_digits = 0
    card_number_reversed = card_number[::1]
    odd_digits = card_number_reversed[::2]

    for digit in odd_digits:
        sum_of_odd_digits += int(digit)

    sum_of_even_digits = 0
    even_digits = card_number_reversed[1::2]
    for digit in even_digits:
        number = int(digit) * 2
        if number >= 10:
            number = (number // 10) + (number % 10)
        sum_of_even_digits += number
    total = sum_of_odd_digits + sum_of_even_digits

    return total % 10 == 0

def main():
    card_number = '4111-1111-4555-1141'
    card_translation = str.maketrans({'-': '', ' ': ''})
    translated_card_number = card_number.translate(card_translation)

    if verify_card_number(translated_card_number):
        pass
    

main()

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Learn How to Work with Numbers and Strings by Implementing the Luhn Algorithm - Step 35

this card_number is the one you need to change

Tell us what’s happening:

I did what I was told to do, but it still not passing the code!

Your code so far


# User Editable Region

def verify_card_number(card_number):
    sum_of_odd_digits = 0
    card_number_reversed = card_number[::-1]
    odd_digits = card_number_reversed[::2]

    for digit in odd_digits:
        sum_of_odd_digits += int(digit)

    sum_of_even_digits = 0
    even_digits = card_number_reversed[1::2]
    for digit in even_digits:
        number = int(digit) * 2
        if number >= 10:
            number = (number // 10) + (number % 10)
        sum_of_even_digits += number
    total = sum_of_odd_digits + sum_of_even_digits
    print(total)
    return total % 10 == 0

def main():
    card_number = '4111-1111-4555-1142'
    card_translation = str.maketrans({'-': '', ' ': ''})
    translated_card_number = card_number.translate(card_translation)

    verify_card_number(translated_card_number)
       
    

main()

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Learn How to Work with Numbers and Strings by Implementing the Luhn Algorithm - Step 35

you removed too many things. Reset the step, find the verify_card_number and remove the print call from inside that, not from inside main.

Tell us what’s happening:

I doesn’t seem to work, even following the instructions!

Your code so far


# User Editable Region

def verify_card_number(card_number):
    sum_of_odd_digits = 0
    card_number_reversed = card_number[::-1]
    odd_digits = card_number_reversed[::2]

    for digit in odd_digits:
        sum_of_odd_digits += int(digit)

    sum_of_even_digits = 0
    even_digits = card_number_reversed[1::2]
    for digit in even_digits:
        number = int(digit) * 2
        if number >= 10:
            number = (number // 10) + (number % 10)
        sum_of_even_digits += number
    total = sum_of_odd_digits + sum_of_even_digits
    print(total)
    return total % 10 == 0

def main():
    card_number = '4111-1111-4556-1141'
    card_translation = str.maketrans({'-': '', ' ': ''})
    translated_card_number = card_number.translate(card_translation)

    if verify_card_number(translated_card_number):
        print ('VALID!')
       
    

main()

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Learn How to Work with Numbers and Strings by Implementing the Luhn Algorithm - Step 35

Do not open duplicate topics.


Did you reset the step?

Which part of your code is the verify_card_number function?

Do not delete anything from the main function.

Thanks ILM, it worked. Thanks to you :+1:

card_number = ‘4111-1111-4555-1141’ the problem is that there was a 2 at the end which they removed from the last step (ie) change the card number to somthing ‘INVALID!’ .
number should be changed to this
card_number = ‘4111-1111-4555-1142’

if that was the only issue it would have been much shorter

my card number matches that of the one in your comment but it doesn’t work for me

hi @Ethan1 , please create your own topic to share your code and ask for help

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.