4

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
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
Brian Upward
  • 59
  • 1
  • 8

2 Answers2

2

You almost got it right. Only that the last digit (or first from behind) should be considered as odd for your 16 digit card. So you should set:

digit = len(cardNumber) - 1

And then your while condition should stop at >= 0 (zeroth item inclusive); note that the len( cardNumber ) == 16 is redundant as the length of the card is constant:

while digit >= 0:

And finally your indexing of the creditcard number will no longer need a minus 1:

value = int(cardNumber[digit]) * 2
...
...
total = total + int(cardNumber[digit])
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
0

So your code is mostly correct, the only issue is that you haven't properly defined what should be considered an "odd" and an "even" number. As you read the number from the end, "odd and even" are also relative from the end, so :

  • odd numbers start from the last one, and then every other one
  • even numbers start from the last but one, and then every other one

Example : 1234 is EOEO, 12345 is OEOEO (O means odd , E means even)

Here the fixed code (I only modified 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
  • 3,033
  • 3
  • 20
  • 32