Below is my code:

[hidden code]

output is: Enter the card number: 378282246310005 firstTwo 37; firstOne 3; cardlen 15 MASTERCARD

note: it shows MASTERCARD instead of AmEx..Why?

1 Answer 1


One thing first, and has higher precedence than or. A or B and C is same as A or (B and C). Keep that in mind when combining the two.

In Python, pretty much like in C, firstTwo == 51 or 52 or 53 or 54 or 55 is interpreted as (firstTwo == 51) or (52) or (53) or (54) or (55), five expressions combined with or. As all but the first (which may or may not) are always truthy, the whole condition will always trigger.

Correct would be for example firstTwo in [51, 52, 53, 54, 55], or 51 <= firstTwo <= 55 (that one did not work in C, where comparison operators cannot be chained).

You must log in to answer this question.

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