0

I am importing credit to python. Here in this part of the code, somehow the if condition never branches out. It is always true for the first condition and I always get AMEX as my output. The string parses correctly and name gets the first 2 characters correctly as well.

Here's the snippet of relevant code

n = cs50.get_string("Number: ")
# taking in initial characters
name = n[ : 2]
name1 = n[ : 1]

# testing for type of card
if name == "34" or "37":
    print("AMEX")
elif name == "51" or "52" or "53" or "54" or "55":
    print("MasterCard")
if name1 == "4":
    print("VISA")

1 Answer 1

1

This might do the trick. Otherwise send the entire code so I can test it out. Below will certainly be needed to make any code work, I hope it solves your particular problem.

if name == "34" or if name == "37": print("AMEX")

elif name == "51" or name == "52" or name == "53" or name == "54" or name == "55": print("MasterCard")

3
  • Hey there! I edited the code like this if name == "34" or name == "37": for all the lines. And viola, it worked! Can you elaborate on why it did work and what was the problem earlier? Apr 7, 2018 at 13:44
  • Hmm, I'm a beginner like you but any statement with 'and' or 'or' is complete only when you set a variable equal to a value, again and again. It is just a syntactic rule.
    – Karin
    Apr 7, 2018 at 13:46
  • Got the answer in this single sentence - When you use or, python sees each side of the operator as separate expressions. Apr 7, 2018 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .