#πŸ”’ Credit card validator understanding problem!

28 messages Β· Page 1 of 1 (latest)

naive stratus
#

Whole code :

Credit card validator by Ahum

credit_card_number = input("Enter your credit card number : \n>")
sum_odd_digits = 0
sum_even_digits = 0
total = 0

Replace any '-' with an empty string

temp_credit_card_number = credit_card_number
temp_credit_card_number = temp_credit_card_number.replace("-", "")
temp_credit_card_number = temp_credit_card_number.replace(" ", "")
temp_credit_card_number = temp_credit_card_number[::-1]

Add all digits in the odd places from right to left

temp_credit_card_number = list(map(int , str(temp_credit_card_number)))

temp_credit_card_number = [int(digit) for digit in temp_credit_card_number]
#no 2

for index, digit in enumerate(temp_credit_card_number):
if index % 2 == 0:
sum_odd_digits += digit

# no3 step
else:
    doubled = digit * 2
    if doubled > 9:
        doubled -= 9
    sum_even_digits += doubled

no4 step

total = sum_even_digits + sum_odd_digits

no5 step

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

Can you please explain the step 2 and 3, the loop step odd digit and even digit?

coral ravenBOT
#

@naive stratus

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mental blade
naive stratus
#

__Whole code : __

#

wait

mental blade
#

just edit the post

naive stratus
#

# Credit card validator by Ahum Maitra

credit_card_number = input("Enter your credit card number : \n>")
sum_odd_digits = 0
sum_even_digits = 0
total = 0

# Replace any '-' with an empty string
temp_credit_card_number = credit_card_number
temp_credit_card_number = temp_credit_card_number.replace("-", "")
temp_credit_card_number = temp_credit_card_number.replace(" ", "")
temp_credit_card_number = temp_credit_card_number[::-1]


# Add all digits in the odd places from right to left
# temp_credit_card_number = list(map(int , str(temp_credit_card_number)))
temp_credit_card_number = [int(digit) for digit in temp_credit_card_number]

for index, digit in enumerate(temp_credit_card_number):
    if index % 2 == 0:
        sum_odd_digits += digit

    # no3 step
    else:
        doubled = digit * 2
        if doubled > 9:
            doubled -= 9
        sum_even_digits += doubled


# no4 step

total = sum_even_digits + sum_odd_digits

# no5 step
if total % 10 == 0:
    print("Valid")
else:
    print("Invalid")

***__I want to understand this part : __***

for index, digit in enumerate(temp_credit_card_number):
    if index % 2 == 0:
        sum_odd_digits += digit

    # no3 step
    else:
        doubled = digit * 2
        if doubled > 9:
            doubled -= 9
        sum_even_digits += doubled

mental blade
#

what's the issue there???

naive stratus
#

It's luhn's algorithm and I don't understand this part - loop (for index , digit.....), how this work enumerate(list)?

#

Please explain it! Visually (if possible)

#

\

maiden sleet
#

Please provide output

naive stratus
#

ok

#

(.venv) PS C:\Users\theah\Documents\Credit card validator in Python\src> python main.py
Enter your credit card number :

0123-4567-8910
Invalid
(.venv) PS C:\Users\theah\Documents\Credit card validator in Python\src>

mental blade
#

@naive stratus what is your question???

#

you want to know fix it or you want to know how it works???

naive stratus
#

I don't understand the loop :

for index, digit in enumerate(temp_credit_card_number):
if index % 2 == 0:
sum_odd_digits += digit

# no3 step
else:
    doubled = digit * 2
    if doubled > 9:
        doubled -= 9
    sum_even_digits += doubled
#

how index and digit works?

mental blade
#

there's no index there

#

!e

sum_odd_digits = 0
sum_even_digits = 0

temp_credit_card_number = list("79927398713")

for index, digit in enumerate(temp_credit_card_number):
    digit = int(digit)
    print(digit, end=" ")
    if index % 2 == 0:
        sum_odd_digits += digit

    # no3 step
    else:
        doubled = digit * 2
        if doubled > 9:
            doubled -= 9
        sum_even_digits += doubled

print(sum_even_digits+sum_odd_digits)
coral ravenBOT
maiden sleet
#

!enumerate

coral ravenBOT
#
The `enumerate` function

Ever find yourself in need of the current iteration number of your for loop? You should use enumerate! Using enumerate, you can turn code that looks like this:

index = 0
for item in my_list:
    print(f"{index}: {item}")
    index += 1

into beautiful, pythonic code:

for index, item in enumerate(my_list):
    print(f"{index}: {item}")

For more information, check out the official docs, or PEP 279.

maiden sleet
#

Basically enumerate adds an index number to every single loop start

#

Not necessary at many times only useful if you want to know the loop number iteration explicitly.

coral ravenBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.