I have began taking the CS50 course on EDX the past couple of days, and one of the tasks to accomplish was to write a credit card validator in C. I wrote it in C, and then I thought that I could go about the same thing in Python, and wrote the same program in Python. For those of you unfamiliar with the problem, here is the description.
Because I took the program and converted from C to Python, it is probably not going to be written in the most Pythonic way. I tried to add some Pythonic ways of doing things, but there are probably many more things I could have done. If you find anything that could be done in a better, faster, or more Pythonic way, please let me know. Thanks.
def main():
# cc_number = int(input("Enter a valid credit card number: "))
cc_number = 12345678912356789
if not checksum(cc_number):
print("INVALID")
else:
print(check_provider(cc_number))
def check_provider(number):
if len(str(number)) < 13 or len(str(number)) > 16:
return 'INVALID'
if ((list(str(number)))[:2]) == ['3', '4'] or ((list(str(number)))[:2]) == ['3', '7']:
return 'AMEX'
elif ((list(str(number)))[:2]) == ['5', '1'] or ((list(str(number)))[:2]) == ['5', '2'] \
or ((list(str(number)))[:2]) == ['5', '3'] or ((list(str(number)))[:2]) == ['5', '4'] \
or ((list(str(number)))[:2]) == ['5', '5']:
return 'MASTERCARD'
elif ((list(str(number)))[0]) == '4':
return 'VISA'
else:
return 'INVALID'
def checksum(number):
list_number = list(str(number))
odd_indices = []
even_indices = []
last_index = len(list_number) - 1
rev_list = list(reversed(list_number))
for i in rev_list[1::2]:
odd_indices.append(i)
for i in rev_list[::2]:
even_indices.append(i)
sum_odd = sum(split_to_digits(''.join(int_to_str(mul(odd_indices, 2)))))
sum_even = sum(split_to_digits(''.join(int_to_str(even_indices))))
s = sum_odd + sum_even
print(s)
if s % 10 == 0:
return True
return False
def mul(list, x):
return [(int(n) * 2) for n in list]
def split_to_digits(n):
return [int(i) for i in str(n)]
def int_to_str(n):
return [str(x) for x in n]
def str_to_int(n):
return [int(x) for x in n]
main()