Hey I am doing Luhn's algorithm for an assignment for school.
A few outputs are coming out the correct way; however, some are not.
0004222222222222
is giving me a total of 44
,
and
0378282246310005
is giving me a total of 48
,
for a few examples.
I know my code isn't the cleanest as I am a novice but if anyone can identify where I'm getting my error I'd really appreciate
Here is my code:
cardNumber = input( "What is your card number? ")
digit = len(cardNumber)
value = 0
total = 0
while ( len( cardNumber ) == 16 and digit > 0):
# HANDLE even digit positions
if ( digit % 2 == 0 ):
value = ( int( cardNumber[digit - 1]) * 2 )
if( value > 9 ):
double = str( value )
value = int( double[:1] ) + int( double[-1] )
total = total + value
value = 0
digit = digit - 1
else:
total = total + value
value = 0
digit = digit - 1
# HANDLE odd digit positions
elif ( digit % 2 != 0):
total = total + int( cardNumber[digit - 1] )
digit = digit - 1