NatalieRenee
NatalieRenee

Reputation: 21

Not sure how to fix this error Luhn Algorithm PYTHON

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

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...

Edit1: Okay, for example when i post 0378282246310005 (a fake american express) it says

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

but when i insert a random number like 0378282246310005 it works

Please enter your credit card number 0378282246310005

We do not accept that kind of card

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)

or say that "we dont support the card"

#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>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")

Upvotes: 2

Views: 410

Answers (1)

Josie Thompson
Josie Thompson

Reputation: 5818

in the control statements under the comment #Figure out what credit card the user has, the variable cardType is defined in every branch except else. 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.

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

if ...:
    ...
else:
    cardType = "some special value"

if cardType == "some special value":
    ...

or you could use a try/except statement:

try:
    print(cardType)
except NameError:
    print("invalid card number")

EDIT: Also you should note that currently the total variable will always be 0 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.

Upvotes: 1

Related Questions