Hey guys, I'm new to this website as well as Python and well, programming in general. Anyway, I would like some feedback on this program I wrote, its purpose is to take a credit card number, which the user inputs and determine whether it is a valid or invalid credit card number. I would like to know how I can improve my code, what I should have done differently and any other advice i could get, thanks!
def getEveryOther(creditCardNum, lst):
for x in creditCardNum:
lst.append(int(x))
length = len(lst) / 2
while length > 0:
length = length - 1
firstEveryOther = lst[-2::-2]
secondEveryOther = lst[-1::-2]
return firstEveryOther, secondEveryOther
def addFirstNums(listOfNums):
y = 0
for x in listOfNums:
multBy2 = x * 2
if multBy2 >= 10:
newDig = str(multBy2)
newDig = int(newDig[1]) + int(newDig[0])
x = newDig
else:
x = multBy2
y = x + y
return y
def addSecondNums(listOfNums):
y = 0
for x in listOfNums:
y = x+y
return y
def checkIfDivBy10(sumOfBoth):
if sumOfBoth % 10 == 0:
return True
else:
return False
def checkCreditCardNum():
creditCardNum = raw_input("Enter a credit card number: ")
if len(creditCardNum) >= 13 and len(creditCardNum) <= 16:
lengthCheck = True
else:
return creditCardNum, False
if lengthCheck == True:
if creditCardNum[0] == '4':
return creditCardNum, 'Visa'
elif creditCardNum[0] == '5':
return creditCardNum, 'MasterCard'
elif creditCardNum[0] == '3' and creditCardNum[1] == '7':
return creditCardNum, 'American Express'
elif creditCardNum [0] == '6':
return creditCardNum, 'Discover'
else:
return creditCardNum, False
else:
return creditCardNum, False
def askUser():
checkAgain = raw_input("Would you like to check another credit card? ")
checkAgain = checkAgain.lower()
if checkAgain == 'y' or checkAgain == 'yes':
main()
elif checkAgain == 'no' or checkAgain == 'n' or checkAgain == 'q' or checkAgain == 'quit':
return None
else:
askUser()
def main():
creditCardNum, creditType = checkCreditCardNum()
lst = []
if creditType == False:
print creditCardNum, "is an invalid credit card number."
return False
firstEveryOther, secondEveryOther = getEveryOther(creditCardNum, lst)
firstNums = addFirstNums(firstEveryOther)
secondNums = addSecondNums(secondEveryOther)
sumOfBoth = firstNums + secondNums
validOrNot = checkIfDivBy10(sumOfBoth)
if validOrNot == False:
print creditCardNum, "is an invalid credit card number."
elif validOrNot == True:
print creditCardNum, "is a valid", creditType, "credit card."
askUser()
main()