Credit card validity check [closed]
Clash 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'))
python algorithm python-3.x
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
add a comment |Â
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'))
python algorithm python-3.x
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
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
add a comment |Â
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'))
python algorithm python-3.x
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
python algorithm python-3.x
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
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
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
add a comment |Â
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
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
Welcome to Code Review. A few basic pointers about programming in python:
- Go through the code style guide as documented in PEP-8.
- Instead of putting comments, make use of docstrings.
- Use test framework for writing a few tests, and use
assert
s instead ofprint()
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
add a comment |Â
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
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
add a comment |Â
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:
- Go through the code style guide as documented in PEP-8.
- Instead of putting comments, make use of docstrings.
- Use test framework for writing a few tests, and use
assert
s instead ofprint()
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
add a comment |Â
up vote
3
down vote
Welcome to Code Review. A few basic pointers about programming in python:
- Go through the code style guide as documented in PEP-8.
- Instead of putting comments, make use of docstrings.
- Use test framework for writing a few tests, and use
assert
s instead ofprint()
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
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Welcome to Code Review. A few basic pointers about programming in python:
- Go through the code style guide as documented in PEP-8.
- Instead of putting comments, make use of docstrings.
- Use test framework for writing a few tests, and use
assert
s instead ofprint()
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
Welcome to Code Review. A few basic pointers about programming in python:
- Go through the code style guide as documented in PEP-8.
- Instead of putting comments, make use of docstrings.
- Use test framework for writing a few tests, and use
assert
s instead ofprint()
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
edited Aug 13 at 19:44
answered Aug 13 at 19:30
hjpotter92
5,05611539
5,05611539
add a comment |Â
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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