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()
``````

A nice effort!
You can simplify your first function ...

``````def getEveryOther(creditCardNum):
mylist = []
for x in creditCardNum:
mylist.append(int(x))
firstEveryOther = mylist[-2::-2]
secondEveryOther = mylist[-1::-2]
return firstEveryOther, secondEveryOther
``````

... and call it with ...
`firstEveryOther, secondEveryOther = getEveryOther(creditCardNum)`

Another simplification ...

``````def askUser():
checkAgain = raw_input("Would you like to check another credit card (yes, no, quit)? ").lower()
if checkAgain[0] == 'y':
main()
elif checkAgain[0] == 'n' or checkAgain[0] == 'q':
return None
else:
askUser()
``````

Thank you very much for your reply! I cleaned up my code based on your suggestions (after looking at my code, I'm not sure why I made my first function act like that) and fixed my main function because it wasn't performing as intended in every situation. I believe everything works now, here's the new code:

``````def getEveryOther(creditCardNum):
lst = []
for x in creditCardNum:
lst.append(int(x))

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 (yes, no, quit)? ").lower()
if checkAgain[0] == 'y':
main()
elif checkAgain[0] == 'n' or checkAgain[0] == 'q':
pass
else:
askUser()

def main():
creditCardNum, creditType = checkCreditCardNum()

if creditType == False:
print creditCardNum, "is an invalid credit card number."
else:
firstEveryOther, secondEveryOther = getEveryOther(creditCardNum)
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()
``````

A simpler way to add up the first two numbers.

``````def addFirstNums(num_in):
multBy2 = num_in*2
y = 0
## convert to string and cut to two digits if more than two
## doesn't matter if one or more digits
newDig = str(multBy2)[:2]
for num in newDig:
y += int(num)
print num_in, multBy2, y

for num in [2, 6, 102]:
addFirstNums(num)
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.