, , , , "" "" . , " " , :
: 1234 - EOEO, 12345 - OEOEO (O , E )
Here's the fixed code (I only changed three lines, see comments):
digit = len(cardNumber)
value = 0
total = 0
while digit > 0: # I removed the length condition
# HANDLE even digit positions
if ( (len(cardNumber)+1-digit) % 2 == 0 ): # <- modification here
value = ( int( cardNumber[digit - 1]) * 2 )
if( value > 9 ):
double = str( value )
value = int( double[:1] ) + int( double[-1] )
total = total + value
digit = digit - 1
else:
total = total + value
digit = digit - 1
# HANDLE odd digit positions
elif ( (len(cardNumber)+1-digit) % 2 != 0): # <- modification here
value=int( cardNumber[digit - 1] )
total = total + int( cardNumber[digit - 1] )
digit = digit - 1
return total
Some tests:
In : '0378282246310005' -> Out : 60
In : '00378282246310005' -> Out : 60
In : '0004222222222222' -> Out : 40
jadsq source
share