Right now when I run this, I get an error saying
"4 is not in list" for line if (numberList.index(digit)-1) % 2 == 0:
Not sure where I went wrong but any tips would be appreciated. It starts at the second to last letter, and the if statement checks if its every other by .index(digit) % 2 == 0
def credit():
cardType = ""
mastercard = ["51", "52", "53", "54", "55"]
american_express = ["34", "37"]
total = 0
while True:
number = input("Enter Valid Credit Card Number: ")
if " " in str(number):
print("INVALID")
continue
number = int(number)
if len(str(number)) == 15 and str(number)[0:2] in american_express:
cardType = "American Express"
break
elif len(str(number)) == 16:
if str(number)[0] == "4":
cardType = "Visa"
elif str(number)[0:2] in mastercard:
cardType = "Mastercard"
break
elif len(str(number)) == 13:
cardType = "Visa"
break
else:
print("INVALID CARD")
continue
numberList = []
for n in str(number):
numberList.append(n)
for digit in numberList[::-2]:
digit = int(digit)
if numberList.index(digit) % 2 == 0:
if (digit*2) >= 10:
total += ((digit*2) / 10)
total += ((digit*2) % 10)
elif (digit*2) < 10:
total += digit
else:
if digit > 10:
total += (digit / 10)
total += (digit % 10)
elif digit < 9:
total += digit
if (total % 10) == 0:
print(cardType)
else:
print("INVALID CARD")
print(total)
credit()