from cs50 import get_string def invalid(): print("INVALID") exit() # check luhn alghorythm def luhn_algo(): nSum = 0 isSecond = False # iterate over all card numbers from back for i in range(ndigits - 1, -1, -1): d = int(user_input[i]) # check if number is every second if isSecond: d *= 2 # check for double digit sums if d >= 10: nSum += int(d / 10) nSum += int(d % 10) else: nSum += d if isSecond: isSecond = False else: isSecond = True if nSum % 10 == 0: return True else: invalid() # check validity of card by using luhn alghoritm def card_check(): beginning = int(user_input[0:2]) if 39 < beginning < 50: if luhn_algo(): print("VISA") elif beginning == 34 or beginning == 37: if luhn_algo(): print("AMEX") elif 50 < beginning < 56: if luhn_algo(): print("MASTERCARD") else: invalid() # takes user input and check if the value is positive number user_input = get_string("Enter credit card number:") try: if int(user_input) < 1: invalid() except: invalid() # calculates the length of input and if correct, continues with card_check ndigits = len(user_input) if ndigits == 15 or ndigits == 13 or ndigits == 16: card_check() else: invalid()