Credit card validity check [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I want to go more in depth, but this is the 'boilerplate'.



Is there any way I can optimize this or use more idiomatic Python code?



#Luhn algorithm - distinguish valid numbers from mistyped or incorrect numbers.
def checkNumber(cc_number=''):
sum_ = 0
parity = len(cc_number) % 2
for i, digit in enumerate([int(x) for x in cc_number]):
if i % 2 == parity:
digit *= 2
if digit > 9:
digit -= 9
sum_ += digit
return sum_ % 10 == 0

def main():
number = input('Input credit card number: ')

if 12 < len(number) < 17:
first_two = number[0:2]
first_four = number[0:4]

vendor = None
if number[0] == 4:
vendor = 'Visa'
elif number[0] == '5' and '0' < number[1] < '6':
vendor = 'Mastercard'
elif number[0] == '6' or first_four == '6011':
vendor = 'Discover'
elif first_two in ('36', '38'):
vendor = "Diners Club"
elif first_two in ('34', '37'):
vendor = 'American Express'

if vendor is not None:
if checkNumber(number):
print(f'This is a valid vendor credit card!')
else:
print(f'This is not a valid vendor credit card.')
else:
print('Unknown vendor.')
else:
print('Sorry, this is not a valid credit card number!')



print(checkNumber('12345'))









share|improve this question















closed as off-topic by hjpotter92, yuri, 200_success, Mast, Incomputable Aug 14 at 11:37


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – hjpotter92, yuri, 200_success, Mast, Incomputable
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    The code looks awfully duplicate of the one on wikipedia: en.wikipedia.org/wiki/Luhn_algorithm#Python
    – hjpotter92
    Aug 13 at 19:11










  • @MasonBose Welcome to Code Review! it appears you have a registered account (as evidenced by the suggested edit), which can be merged with your unregistered account. You can use the contact SE page and request the accounts be merged.
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 13 at 22:37

















up vote
2
down vote

favorite












I want to go more in depth, but this is the 'boilerplate'.



Is there any way I can optimize this or use more idiomatic Python code?



#Luhn algorithm - distinguish valid numbers from mistyped or incorrect numbers.
def checkNumber(cc_number=''):
sum_ = 0
parity = len(cc_number) % 2
for i, digit in enumerate([int(x) for x in cc_number]):
if i % 2 == parity:
digit *= 2
if digit > 9:
digit -= 9
sum_ += digit
return sum_ % 10 == 0

def main():
number = input('Input credit card number: ')

if 12 < len(number) < 17:
first_two = number[0:2]
first_four = number[0:4]

vendor = None
if number[0] == 4:
vendor = 'Visa'
elif number[0] == '5' and '0' < number[1] < '6':
vendor = 'Mastercard'
elif number[0] == '6' or first_four == '6011':
vendor = 'Discover'
elif first_two in ('36', '38'):
vendor = "Diners Club"
elif first_two in ('34', '37'):
vendor = 'American Express'

if vendor is not None:
if checkNumber(number):
print(f'This is a valid vendor credit card!')
else:
print(f'This is not a valid vendor credit card.')
else:
print('Unknown vendor.')
else:
print('Sorry, this is not a valid credit card number!')



print(checkNumber('12345'))









share|improve this question















closed as off-topic by hjpotter92, yuri, 200_success, Mast, Incomputable Aug 14 at 11:37


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – hjpotter92, yuri, 200_success, Mast, Incomputable
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    The code looks awfully duplicate of the one on wikipedia: en.wikipedia.org/wiki/Luhn_algorithm#Python
    – hjpotter92
    Aug 13 at 19:11










  • @MasonBose Welcome to Code Review! it appears you have a registered account (as evidenced by the suggested edit), which can be merged with your unregistered account. You can use the contact SE page and request the accounts be merged.
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 13 at 22:37













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I want to go more in depth, but this is the 'boilerplate'.



Is there any way I can optimize this or use more idiomatic Python code?



#Luhn algorithm - distinguish valid numbers from mistyped or incorrect numbers.
def checkNumber(cc_number=''):
sum_ = 0
parity = len(cc_number) % 2
for i, digit in enumerate([int(x) for x in cc_number]):
if i % 2 == parity:
digit *= 2
if digit > 9:
digit -= 9
sum_ += digit
return sum_ % 10 == 0

def main():
number = input('Input credit card number: ')

if 12 < len(number) < 17:
first_two = number[0:2]
first_four = number[0:4]

vendor = None
if number[0] == 4:
vendor = 'Visa'
elif number[0] == '5' and '0' < number[1] < '6':
vendor = 'Mastercard'
elif number[0] == '6' or first_four == '6011':
vendor = 'Discover'
elif first_two in ('36', '38'):
vendor = "Diners Club"
elif first_two in ('34', '37'):
vendor = 'American Express'

if vendor is not None:
if checkNumber(number):
print(f'This is a valid vendor credit card!')
else:
print(f'This is not a valid vendor credit card.')
else:
print('Unknown vendor.')
else:
print('Sorry, this is not a valid credit card number!')



print(checkNumber('12345'))









share|improve this question















I want to go more in depth, but this is the 'boilerplate'.



Is there any way I can optimize this or use more idiomatic Python code?



#Luhn algorithm - distinguish valid numbers from mistyped or incorrect numbers.
def checkNumber(cc_number=''):
sum_ = 0
parity = len(cc_number) % 2
for i, digit in enumerate([int(x) for x in cc_number]):
if i % 2 == parity:
digit *= 2
if digit > 9:
digit -= 9
sum_ += digit
return sum_ % 10 == 0

def main():
number = input('Input credit card number: ')

if 12 < len(number) < 17:
first_two = number[0:2]
first_four = number[0:4]

vendor = None
if number[0] == 4:
vendor = 'Visa'
elif number[0] == '5' and '0' < number[1] < '6':
vendor = 'Mastercard'
elif number[0] == '6' or first_four == '6011':
vendor = 'Discover'
elif first_two in ('36', '38'):
vendor = "Diners Club"
elif first_two in ('34', '37'):
vendor = 'American Express'

if vendor is not None:
if checkNumber(number):
print(f'This is a valid vendor credit card!')
else:
print(f'This is not a valid vendor credit card.')
else:
print('Unknown vendor.')
else:
print('Sorry, this is not a valid credit card number!')



print(checkNumber('12345'))






python algorithm python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 13 at 22:59









Jamal♦

30.1k11114225




30.1k11114225










asked Aug 13 at 18:30









Mason Bose

192




192




closed as off-topic by hjpotter92, yuri, 200_success, Mast, Incomputable Aug 14 at 11:37


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – hjpotter92, yuri, 200_success, Mast, Incomputable
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by hjpotter92, yuri, 200_success, Mast, Incomputable Aug 14 at 11:37


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – hjpotter92, yuri, 200_success, Mast, Incomputable
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 3




    The code looks awfully duplicate of the one on wikipedia: en.wikipedia.org/wiki/Luhn_algorithm#Python
    – hjpotter92
    Aug 13 at 19:11










  • @MasonBose Welcome to Code Review! it appears you have a registered account (as evidenced by the suggested edit), which can be merged with your unregistered account. You can use the contact SE page and request the accounts be merged.
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 13 at 22:37













  • 3




    The code looks awfully duplicate of the one on wikipedia: en.wikipedia.org/wiki/Luhn_algorithm#Python
    – hjpotter92
    Aug 13 at 19:11










  • @MasonBose Welcome to Code Review! it appears you have a registered account (as evidenced by the suggested edit), which can be merged with your unregistered account. You can use the contact SE page and request the accounts be merged.
    – Sᴀᴍ Onᴇᴌᴀ
    Aug 13 at 22:37








3




3




The code looks awfully duplicate of the one on wikipedia: en.wikipedia.org/wiki/Luhn_algorithm#Python
– hjpotter92
Aug 13 at 19:11




The code looks awfully duplicate of the one on wikipedia: en.wikipedia.org/wiki/Luhn_algorithm#Python
– hjpotter92
Aug 13 at 19:11












@MasonBose Welcome to Code Review! it appears you have a registered account (as evidenced by the suggested edit), which can be merged with your unregistered account. You can use the contact SE page and request the accounts be merged.
– Sᴀᴍ Onᴇᴌᴀ
Aug 13 at 22:37





@MasonBose Welcome to Code Review! it appears you have a registered account (as evidenced by the suggested edit), which can be merged with your unregistered account. You can use the contact SE page and request the accounts be merged.
– Sᴀᴍ Onᴇᴌᴀ
Aug 13 at 22:37











2 Answers
2






active

oldest

votes

















up vote
3
down vote













Welcome to Code Review. A few basic pointers about programming in python:



  1. Go through the code style guide as documented in PEP-8.

  2. Instead of putting comments, make use of docstrings.

  3. Use test framework for writing a few tests, and use asserts instead of print() for checks and validations.


From the PEP-8 rules:



  • Convert the comment about Luhn's algortihm into a docstring.

  • Variables and functions are named using snake_case convention.

  • No unnecessary blank lines are needed. Keep the code clean and concise.


Your main function can be broken further, into get_provider, valid_card_number and the one your already have check_number. It is completely your choice if you want to raise an exception in case of wrong inputs or whether the functions should return falsy values.



Instead of having an approximately 10 line of code inside if-statement, deal with the else clause first, and return early.



While you didn't actually write the check_number code, I'll just put my views about that as well here.



Using a list comprehension, with the help of % 9 ($ mod 9 $), you can make it quite pythonic. You can then have a sum(<list comprehension>) % 10 == 0 as a one-liner.



sum(digit * 2 % 9 if i % 2 else digit for i, digit in enumerate(map(int, cc_number[::-1]))) % 10 == 0





share|improve this answer





























    up vote
    1
    down vote













    just sometimes some expressions can be assigned to a variable to prevent recalculation like number[0] can be replaced with first_num



    from



    if number[0] == 4:
    vendor = 'Visa'
    elif number[0] == '5' and '0' < number[1] < '6':
    vendor = 'Mastercard'
    elif number[0] == '6' or first_four == '6011':
    vendor = 'Discover'


    to



    first_num = number[0]
    if first_num == 4:
    vendor = 'Visa'
    elif first_num == '5' and '0' < number[1] < '6':
    vendor = 'Mastercard'
    elif first_num == '6' or first_four == '6011':
    vendor = 'Discover'


    also, better snake_case in python than CamelCase



    from



    def checkNumber(cc_number=''):


    to



    def check_number(cc_number=''):


    also, sum can be replaced by a synonym to avoid in-bilt clash thus no need of _



    from



    sum_ = 0


    to



    total = 0





    share|improve this answer


















    • 1




      sum is still a library function: devdocs.io/python~2.7/library/functions#sum
      – hjpotter92
      Aug 13 at 19:31










    • @hjpotter92 ammended
      – Abdur-Rahmaan Janhangeer
      Aug 13 at 19:35

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    Welcome to Code Review. A few basic pointers about programming in python:



    1. Go through the code style guide as documented in PEP-8.

    2. Instead of putting comments, make use of docstrings.

    3. Use test framework for writing a few tests, and use asserts instead of print() for checks and validations.


    From the PEP-8 rules:



    • Convert the comment about Luhn's algortihm into a docstring.

    • Variables and functions are named using snake_case convention.

    • No unnecessary blank lines are needed. Keep the code clean and concise.


    Your main function can be broken further, into get_provider, valid_card_number and the one your already have check_number. It is completely your choice if you want to raise an exception in case of wrong inputs or whether the functions should return falsy values.



    Instead of having an approximately 10 line of code inside if-statement, deal with the else clause first, and return early.



    While you didn't actually write the check_number code, I'll just put my views about that as well here.



    Using a list comprehension, with the help of % 9 ($ mod 9 $), you can make it quite pythonic. You can then have a sum(<list comprehension>) % 10 == 0 as a one-liner.



    sum(digit * 2 % 9 if i % 2 else digit for i, digit in enumerate(map(int, cc_number[::-1]))) % 10 == 0





    share|improve this answer


























      up vote
      3
      down vote













      Welcome to Code Review. A few basic pointers about programming in python:



      1. Go through the code style guide as documented in PEP-8.

      2. Instead of putting comments, make use of docstrings.

      3. Use test framework for writing a few tests, and use asserts instead of print() for checks and validations.


      From the PEP-8 rules:



      • Convert the comment about Luhn's algortihm into a docstring.

      • Variables and functions are named using snake_case convention.

      • No unnecessary blank lines are needed. Keep the code clean and concise.


      Your main function can be broken further, into get_provider, valid_card_number and the one your already have check_number. It is completely your choice if you want to raise an exception in case of wrong inputs or whether the functions should return falsy values.



      Instead of having an approximately 10 line of code inside if-statement, deal with the else clause first, and return early.



      While you didn't actually write the check_number code, I'll just put my views about that as well here.



      Using a list comprehension, with the help of % 9 ($ mod 9 $), you can make it quite pythonic. You can then have a sum(<list comprehension>) % 10 == 0 as a one-liner.



      sum(digit * 2 % 9 if i % 2 else digit for i, digit in enumerate(map(int, cc_number[::-1]))) % 10 == 0





      share|improve this answer
























        up vote
        3
        down vote










        up vote
        3
        down vote









        Welcome to Code Review. A few basic pointers about programming in python:



        1. Go through the code style guide as documented in PEP-8.

        2. Instead of putting comments, make use of docstrings.

        3. Use test framework for writing a few tests, and use asserts instead of print() for checks and validations.


        From the PEP-8 rules:



        • Convert the comment about Luhn's algortihm into a docstring.

        • Variables and functions are named using snake_case convention.

        • No unnecessary blank lines are needed. Keep the code clean and concise.


        Your main function can be broken further, into get_provider, valid_card_number and the one your already have check_number. It is completely your choice if you want to raise an exception in case of wrong inputs or whether the functions should return falsy values.



        Instead of having an approximately 10 line of code inside if-statement, deal with the else clause first, and return early.



        While you didn't actually write the check_number code, I'll just put my views about that as well here.



        Using a list comprehension, with the help of % 9 ($ mod 9 $), you can make it quite pythonic. You can then have a sum(<list comprehension>) % 10 == 0 as a one-liner.



        sum(digit * 2 % 9 if i % 2 else digit for i, digit in enumerate(map(int, cc_number[::-1]))) % 10 == 0





        share|improve this answer














        Welcome to Code Review. A few basic pointers about programming in python:



        1. Go through the code style guide as documented in PEP-8.

        2. Instead of putting comments, make use of docstrings.

        3. Use test framework for writing a few tests, and use asserts instead of print() for checks and validations.


        From the PEP-8 rules:



        • Convert the comment about Luhn's algortihm into a docstring.

        • Variables and functions are named using snake_case convention.

        • No unnecessary blank lines are needed. Keep the code clean and concise.


        Your main function can be broken further, into get_provider, valid_card_number and the one your already have check_number. It is completely your choice if you want to raise an exception in case of wrong inputs or whether the functions should return falsy values.



        Instead of having an approximately 10 line of code inside if-statement, deal with the else clause first, and return early.



        While you didn't actually write the check_number code, I'll just put my views about that as well here.



        Using a list comprehension, with the help of % 9 ($ mod 9 $), you can make it quite pythonic. You can then have a sum(<list comprehension>) % 10 == 0 as a one-liner.



        sum(digit * 2 % 9 if i % 2 else digit for i, digit in enumerate(map(int, cc_number[::-1]))) % 10 == 0






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 13 at 19:44

























        answered Aug 13 at 19:30









        hjpotter92

        5,05611539




        5,05611539






















            up vote
            1
            down vote













            just sometimes some expressions can be assigned to a variable to prevent recalculation like number[0] can be replaced with first_num



            from



            if number[0] == 4:
            vendor = 'Visa'
            elif number[0] == '5' and '0' < number[1] < '6':
            vendor = 'Mastercard'
            elif number[0] == '6' or first_four == '6011':
            vendor = 'Discover'


            to



            first_num = number[0]
            if first_num == 4:
            vendor = 'Visa'
            elif first_num == '5' and '0' < number[1] < '6':
            vendor = 'Mastercard'
            elif first_num == '6' or first_four == '6011':
            vendor = 'Discover'


            also, better snake_case in python than CamelCase



            from



            def checkNumber(cc_number=''):


            to



            def check_number(cc_number=''):


            also, sum can be replaced by a synonym to avoid in-bilt clash thus no need of _



            from



            sum_ = 0


            to



            total = 0





            share|improve this answer


















            • 1




              sum is still a library function: devdocs.io/python~2.7/library/functions#sum
              – hjpotter92
              Aug 13 at 19:31










            • @hjpotter92 ammended
              – Abdur-Rahmaan Janhangeer
              Aug 13 at 19:35














            up vote
            1
            down vote













            just sometimes some expressions can be assigned to a variable to prevent recalculation like number[0] can be replaced with first_num



            from



            if number[0] == 4:
            vendor = 'Visa'
            elif number[0] == '5' and '0' < number[1] < '6':
            vendor = 'Mastercard'
            elif number[0] == '6' or first_four == '6011':
            vendor = 'Discover'


            to



            first_num = number[0]
            if first_num == 4:
            vendor = 'Visa'
            elif first_num == '5' and '0' < number[1] < '6':
            vendor = 'Mastercard'
            elif first_num == '6' or first_four == '6011':
            vendor = 'Discover'


            also, better snake_case in python than CamelCase



            from



            def checkNumber(cc_number=''):


            to



            def check_number(cc_number=''):


            also, sum can be replaced by a synonym to avoid in-bilt clash thus no need of _



            from



            sum_ = 0


            to



            total = 0





            share|improve this answer


















            • 1




              sum is still a library function: devdocs.io/python~2.7/library/functions#sum
              – hjpotter92
              Aug 13 at 19:31










            • @hjpotter92 ammended
              – Abdur-Rahmaan Janhangeer
              Aug 13 at 19:35












            up vote
            1
            down vote










            up vote
            1
            down vote









            just sometimes some expressions can be assigned to a variable to prevent recalculation like number[0] can be replaced with first_num



            from



            if number[0] == 4:
            vendor = 'Visa'
            elif number[0] == '5' and '0' < number[1] < '6':
            vendor = 'Mastercard'
            elif number[0] == '6' or first_four == '6011':
            vendor = 'Discover'


            to



            first_num = number[0]
            if first_num == 4:
            vendor = 'Visa'
            elif first_num == '5' and '0' < number[1] < '6':
            vendor = 'Mastercard'
            elif first_num == '6' or first_four == '6011':
            vendor = 'Discover'


            also, better snake_case in python than CamelCase



            from



            def checkNumber(cc_number=''):


            to



            def check_number(cc_number=''):


            also, sum can be replaced by a synonym to avoid in-bilt clash thus no need of _



            from



            sum_ = 0


            to



            total = 0





            share|improve this answer














            just sometimes some expressions can be assigned to a variable to prevent recalculation like number[0] can be replaced with first_num



            from



            if number[0] == 4:
            vendor = 'Visa'
            elif number[0] == '5' and '0' < number[1] < '6':
            vendor = 'Mastercard'
            elif number[0] == '6' or first_four == '6011':
            vendor = 'Discover'


            to



            first_num = number[0]
            if first_num == 4:
            vendor = 'Visa'
            elif first_num == '5' and '0' < number[1] < '6':
            vendor = 'Mastercard'
            elif first_num == '6' or first_four == '6011':
            vendor = 'Discover'


            also, better snake_case in python than CamelCase



            from



            def checkNumber(cc_number=''):


            to



            def check_number(cc_number=''):


            also, sum can be replaced by a synonym to avoid in-bilt clash thus no need of _



            from



            sum_ = 0


            to



            total = 0






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 13 at 19:35

























            answered Aug 13 at 19:05









            Abdur-Rahmaan Janhangeer

            21811




            21811







            • 1




              sum is still a library function: devdocs.io/python~2.7/library/functions#sum
              – hjpotter92
              Aug 13 at 19:31










            • @hjpotter92 ammended
              – Abdur-Rahmaan Janhangeer
              Aug 13 at 19:35












            • 1




              sum is still a library function: devdocs.io/python~2.7/library/functions#sum
              – hjpotter92
              Aug 13 at 19:31










            • @hjpotter92 ammended
              – Abdur-Rahmaan Janhangeer
              Aug 13 at 19:35







            1




            1




            sum is still a library function: devdocs.io/python~2.7/library/functions#sum
            – hjpotter92
            Aug 13 at 19:31




            sum is still a library function: devdocs.io/python~2.7/library/functions#sum
            – hjpotter92
            Aug 13 at 19:31












            @hjpotter92 ammended
            – Abdur-Rahmaan Janhangeer
            Aug 13 at 19:35




            @hjpotter92 ammended
            – Abdur-Rahmaan Janhangeer
            Aug 13 at 19:35


            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay