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

Tell us what’s happening:

I know the answer is :
—solution removed—

I do not understand where the first and second characters come from. If X is the first number in the starting index and the Y is supposed to be the ending index.

Why is the answer not:
card_number_reversed=card_number[-2: -1:-1]
because in reverse the four numbers are -2(first), -4, -1, -1(last)

Your code so far


# User Editable Region

def verify_card_number(card_number):
    sum_of_odd_digits = 0
    card_number_reversed = card_number[0:4:2]
    print(card_number_reversed)
    

# User Editable Region

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

    print(translated_card_number)

    verify_card_number(translated_card_number)

main()

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

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

Those numbers refer to the positions, not the actual numbers:

https://www.geeksforgeeks.org/how-to-index-and-slice-strings-in-python/

The position of the last number in a string is -1.

string = "Tube"
string[3] 
string[-1]

string[3] and string[-1] both access the “e” letter in the string.

1 Like

Okay thank you for the link as well.