2

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")
3
  • 1
    Please clarify what exactly is not working right now? Show the full stack trace. If you don't have any errors, what output are you getting that does not meet your expectation? You want to put together a minimal reproducible example to help the reader know how to help you. Commented Oct 17, 2016 at 3:57
  • I added the edits ! thanks Commented Oct 17, 2016 at 4:00
  • This is a minor nitpick, and not really relevant to your question, but you should consider reserving CapitalCaseNames for class names and not variable names. It's a standard in almost every language. Commented Oct 17, 2016 at 5:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

okay that makes sense, but now i am confused. How would I let python know that "else" means anything that is not listed above? I thought that was the point of "else"? I know i'm thinking about the wrong way :/
@NatalieRenee Just create a default for cardType before your conditional statements. Maybe an example can be provided in the answer to provide some clarity
or maybe my answer wasn't clear. I'll change it to show some examples of how to fix it
You just need to define cardType in the else statement, such as cardType = 'Your card is'
The better approach is to actually declare the variable before entering the conditional statements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.