I am doing a self-paced course, CS50 on EDX platform, the goal is to ask a user for a credit card number and then validate if it's a valid Amex, Master or Visa card.
I'm stuck trying split a number greater than 10 (formatted as a String) so that the result is for example "14" = "1" and "4" the idea is that both numbers will be appended to a list[].
I am trying to implement a credit card checksum check based on the algorithm invented by Hans Peter Luhn, I am stuck in the second part of the algorithm (written as a comment on the code) because as the algorithm specifies I have to split 14 into 1 4 and THEN do the sum.
Credit card for test: 378282246310005 So far i have this result: 14 + 4 + 4 + 8 + 6 + 0 + 0 = 36 //Bad and what i need to keep going is: 1 + 4 + 4 + 4 + 8 + 6 + 0 + 0 = 27 //Good
Here is my code:
def main():
cCNumber = str(input("Ingrese el numero de la tarjeta de credito: "))
if cCNumber[0] == "3" and (cCNumber[1] == "4" or cCNumber[1] == "7") :
numbers = listInts(cCNumber)
listSumador = checkSum(numbers)
listSumadorStr = str(listSumador)
print(listSumadorStr)
print("Amex")
#Convert string to a list of Ints.
def listInts(stRnumber):
listNumbers = []
for i in range(0,len(stRnumber),1):
listNumbers.append(int(stRnumber[i]))
return listNumbers
#Implement the following algorithm:
#1: Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.
#2: Add the sum to the sum of the digits that weren’t multiplied by 2.
#3: if the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!
def checkSum(numbers):
sumador = []
for i in range(1,len(numbers),2):
provisoryList = []
provisoryList.append(int(numbers[i]) * 2)
sumador.append(int(numbers[i]) * 2)
return sumador
if __name__ == "__main__":
main()