
# convert CCN to list
def ccnToList(ccnStr):
    ccnList =[]
    for i in ccnStr:
    	ccnList.append(int(i))
    print "list ccn:", ccnList
    return ccnList

		
# check length and prefix of CCN list to see if it matches the length and prefix rules for the 3 card types
# return string "invalid" or string containing name of card type
def check_length_prefix(ccn):
    flag = False
    cardType = "invalid"
    if(len(ccn) == 15):
    	if ccn[0:2] == [3, 4] or ccn[0:2] == 37:
	   flag = True
	   cardType = "American Express"
    elif len(ccn) == 16:
    	 if ccn[0] == 4:
	    cardType = "Visa"
	    flag = True
    elif ccn[0] == 5 and (ccn[1] >= 1 and ccn[1] <=5):
	     flag = True
	     cardType = "Master Card"
    return cardType
	     
    	 
# take credit card number list and return a list containing the digits in reverse
def reverse_List(ccn):
    rev =[]
    for i in range(len(ccn)-1, -1, -1):
    	rev.append(ccn[i])
    print "reversed: ", rev
    return rev


# perform Luhn algorithm on CCN list
def luhn(ccn):
    # reverse the list
    rev = reverse_List(ccn)
    doubled = []
    for i in range(0, len(rev)):
    	if i %2 == 0:
	   doubled.append(rev[i])
        else:
		doubled.append(2*rev[i])
    print "doubled: ", doubled
    return luhn_sum(doubled)


# return sum of digits in list
def luhn_sum(dbl):
    sum = 0
    for i in dbl:
    	if i > 9:
	   sum += i%10
	   sum += i/10
        else:
	   sum += i
    print "sum:", sum
    return sum

def main():
    # read CCN number

    ccnNumber = raw_input("Please enter the CCN with no dashes: ")

    if ccnNumber.isdigit():
        ccn = ccnToList(ccnNumber)
        if check_length_prefix(ccn) == "invalid":
            print "Invalid"
        else:
            if luhn(ccn) % 10 == 0:
                print check_length_prefix(ccn)
            else: 
                print "Invalid"


main()
        
       
    	
    


