Skip to content

Instantly share code, notes, and snippets.

@ianjmacintosh
Created May 29, 2020 00:17
Show Gist options
  • Select an option

  • Save ianjmacintosh/1d5ed6feaac64b6149a210d2f8b7d6b5 to your computer and use it in GitHub Desktop.

Select an option

Save ianjmacintosh/1d5ed6feaac64b6149a210d2f8b7d6b5 to your computer and use it in GitHub Desktop.
from cs50 import get_string
def main():
card_number = get_string("Number: ")
length = len(card_number)
if not is_valid(card_number):
print("INVALID")
else:
if length == 15 and (card_number[0:2] == "34" or card_number[0:2] == "37"):
print("AMEX")
elif length == 16 and (int(card_number[0:2]) >= 51 and int(card_number[0:2]) <= 55):
print("MASTERCARD")
elif (length == 13 or length == 16) and (card_number[0:1] == "4"):
print("VISA")
def is_valid(number):
index = 0
sum_of_even_numbers = 0
sum_of_odd_numbers = 0
for digit in number:
index += 1
digit = int(digit)
if (len(number) - index) % 2 == 0:
# Even numbers: Add to total
sum_of_even_numbers += digit
else:
# Odd number: Multiply by 2, sum product's individual digits, add to total
digit *= 2
if digit > 9:
digit = int(str(digit)[0:1]) + int(str(digit)[1:2])
sum_of_odd_numbers += digit
if str(sum_of_even_numbers + sum_of_odd_numbers)[1:2] == "0":
return True
return False
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment