0

I'm new to python and currently working on implementing Luhns algo in python. I'm already done with calculation of checksum. Now I'm facing challenge in identifying the card company using if and elif statement.

Below is my code block to identify the card company. The problem I'm facing is else if statements are not getting executed even if the conditions are true and, in some cases, even ELSE within IF is not being executed. This happens whenever there's an OR condition to compare with multiple values.

I've tried moving the full conditions within parenthesis((first_2 == (34 or 37) and length==15) but the result is same.

Can't we use multiple conditions in if or elif statement OR is there any other way compare multiple values for if statement?

Thank You

Code>>>

if (digit+number_add2) % 10 ==0 and length >= 13:#check if remainder is Zero
    if first_2 == (34 or 37) and length==15:#Check first 2 digits and length
       print("AMEX")
    elif first_2 == (51 or 52 or 53 or 54 or 55) and length==16:
       print("MASTERCARD")
    elif first_1== 4 and length == (13 or 16):
       print("VISA")
    else:
       print("INVALID1")#Invalid if number passes Luhns algo but card is not from Visa,MC,Amex 
else:
    print("INVALID2")#Invalid if card number does not adhere Luhn's algo
Lavakara
  • 1
  • 1
  • 1
    Use `in`. For example, `if first_2 in [34, 37] and length==15:`. – MattDMo Aug 28 '22 at 14:45
  • Yes this work, But in this case can we use > < == conditions? – Lavakara Aug 28 '22 at 14:49
  • Because Python calculates *first* the expression in parentheses and *then* it compares. If you want to use `or`, you should write `(first_2 == 51) or (first_2 == 52) or ...`. – Matthias Aug 28 '22 at 15:41

0 Answers0