Luhn's Algorithm Pseudocode to code
<p>Hey guys I'm fairly new to the programming world. For a school practice question I was given the following text and I'm suppose to convert this into code. I've spent hours on it and still can't seem to figure it out but I'm determine to learn this. I'm currently getting the error</p>

<pre><code>  line 7, in &lt;module&gt; if i % 2 == 0: TypeError: not all arguments converted during string formatting 
</code></pre>

<p>What does this mean? I'm still learning loops and I'm not sure if it's in the correct format or not. Thanks for your time. </p>

<pre><code># GET user's credit card number
# SET total to 0
# LOOP backwards from the last digit to the first one at a time
    # IF the position of the current digit is even THEN
        # DOUBLE the value of the current digit
        # IF the doubled value is more than 9 THEN
            # SUM the digits of the doubled value
        # ENDIF
       # SUM the calculated value and the total
    # ELSE
        # SUM the current digit and the total
    # ENDIF
# END loop
# IF total % 10 == 0 THEN
    # SHOW Number is valid
# ELSE
    # SHOW number is invalid
# ENDIF


creditCard = input("What is your creditcard?")
total = 0
for i in creditCard[-1]:
    if i % 2 == 0:
        i = i * 2
        if i &gt; 9:
            i += i
    total = total + i

    else:
        total = total + i

if total % 10 == 0:
    print("Valid")

else:
    print("Invalid")
</code></pre>

<p>well, i can see 2 problems:</p>

<p>1)when you do:</p>

<pre><code>for i in creditCard[-1]
</code></pre>

<p>you dont iterate on the creditCard you simply take the last digit.
you probably meant to do </p>

<pre><code>for i in creditCard[::-1]
</code></pre>

<p>this will iterate the digits from the last one to the first one</p>

<p>2)
the pseudocode said to double the number if its POSITION is even, not if the digit itself is even</p>

<p>so you can do this:</p>

<pre><code>digit_count = len(creditCard)
for i in range(digit_count -1, -1, -1):
    digit = creditCard[i]
</code></pre>

<p>or have a look at the <code>enumerate</code>  built-in function</p>

<p>edit:</p>

<p>complete sample:</p>

<pre><code>creditCard = input("What is your creditcard?")
total = 0
digit_count = len(creditCard)
for i in range(0, digit_count, -1):
    digit = creditCard[i]

    if i % 2 == 0:
        digit = digit  * 2
        if digit  &gt; 9:
            digit = digit / 10 + digit % 10 # also noticed you didnt sum the digits of the number  

    total = total + digit



if total % 10 == 0:
    print("Valid")

else:
    print("Invalid")
</code></pre>

python