Not sure how to fix this error Luhn Algorithm PYTHON
<p>Alright,
So I think i'm almost there,
my first/second part work perfect when they are on their own, but i'm having trouble combining the two
this is what I have so far
I'm thinking the error is in the last bit, 
sorry, im new with python, so i'm hoping to get the hang of it soon</p>

<p>Edit3: i've gotten it to work (with the help of you guys) but now when i input 3782822463100050, its suppose to be invalid american express, but its showing up as valid american express...</p>

<p>Edit1: Okay, for example when i post <code>0378282246310005</code> (a fake american express) it says </p>

<pre><code>Traceback (most recent call last):
  File "C:/Users/Nat/Desktop/attempt.py", line 39, in &lt;module&gt;
    print((cardType)+"Valid")
NameError: name 'cardType' is not defined
</code></pre>

<p>but when i insert a random number like <code>0378282246310005</code> it works</p>

<p>Please enter your credit card number 0378282246310005</p>

<p>We do not accept that kind of card</p>

<p>Edit2: in the end you should be able to type in a credit card number and it'll say "Your "type of credit card" is valid (or invalid)</p>

<p>or say that "we dont support the card"</p>

<pre><code>#GET number that will be tested
CreditNumber = input("Please enter your credit card number")

#SET total to 0
total=0

#LOOP backwards from the last digit to the first, one at a time
CreditCount = len(CreditNumber)
for i in range(0, CreditCount, -1):
    LastDigit=CreditCard[i]

#IF the position of the current digit is even THEN DOUBLE the value of the current digit
    if i % 2 ==0:
        LastDigit=LastDigit*2

#IF the doubled value is more than 9 THEN SUM the digits of the doubled value
        if LastDigit&gt;9:
            LastDigit=LastDigit/10+LastDigit%10

    total=total + digit

#Figure out what credit card the user has
if ( CreditNumber[0:2]=="34" or CreditNumber[ 0:2 ] == "37"):
     cardType = "Your American Express is"

elif ( CreditNumber[ 0 :4 ] =="6011"):
       cardType = "Your Discover card is"

elif ( CreditNumber[0 :2 ]  in [ "51", "52", "53", "54", "55"]):
       cardType = "Your Mastercard is"

elif ( CreditNumber == "4" ):
       cardType = "Your VISA card is"

else:
       print( "We do not accept that kind of card")

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

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

<p>in the control statements under the comment  <code>#Figure out what credit card the user has</code>, the variable <code>cardType</code> is defined in every branch except <code>else</code>. Since the name was never defined outside the scope of the control statement, the interpreter gives you a NameError when you try to access the variable when the code followed the else branch of the if statement.</p>

<p>to fix this you can do a couple of different things. you can create a a special value for <code>cardType</code> when <code>CardNumber</code> is invalid and check for it in the next control statement:</p>

<pre><code>if ...:
    ...
else:
    cardType = "some special value"

if cardType == "some special value":
    ...
</code></pre>

<p>or you could use a try/except statement:</p>

<pre><code>try:
    print(cardType)
except NameError:
    print("invalid card number")
</code></pre>

<p><strong>EDIT</strong>: Also you should note that currently the <code>total</code> variable will always be <code>0</code> as the for loop doesn't actually run. If you want to decrement a range, the first argument should be greater than the second, or the range function will just create an empty list.</p>

python luhn