Validating Credit Card Numbers

  • + 0 comments

    Enter your code here. Read input from STDIN. Print output to STDOUT

    def check_no_of_digits(p): digits_list = [i for i in p if i.isdigit()] if len(digits_list) == 16: return True return False def check_starting_digits(q): if q[0]=='4' or q[0]=='5' or q[0]=='6': return True return False def check_digits(r): for i in r: if not i.isdigit(): return False return True def check_hyphens(s): if "-" not in s: return True else: l = s.split("-") for i in l: if len(i)!=4: return False return True def consecutive_check(t): lst = [i for i in t if i.isdigit()] #print(lst) for i in range(len(lst) - 3): # Ensure we check till len(lst) - 4 if lst[i] == lst[i+1] == lst[i+2] == lst[i+3]: return True return False n = int(input()) c = 0 for i in range(n): x = input() y = check_no_of_digits(x) z = check_starting_digits(x) a = check_digits(x) b = check_hyphens(x) c = consecutive_check(x) if y and z and a and not c and b: print("Valid") else: print("Invalid")