Strange behavior of if elif condition in Python [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 6 months ago.
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

Related

How to get elif to work in code? Do I need the use elif? [duplicate]

This question already has answers here:
Python SyntaxError: invalid syntax elif statement [closed]
(3 answers)
python : if statement and multiple variable values [duplicate]
(2 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 5 months ago.
I'm trying to have the user enter their name and client stock profit information for as many clients as the user wants.The loop will go around until the user answers no for if they have another client they want to enter. Then it will print the average of all the clients stock information. I'm just not sure how to get the loop to stop properly and execute the rest of the code.
csum=0
ccount=0
clients=int(input("How Many Clients?"))
while clients==clients:
sum=0
counter=0
determiner=str(input("Do you have another client to enter?"))
clientname=str(input("What is the client's name?"))
if determiner==str('Yes' or "Y" or "YES"):
for i in range(4):
stock=int(input("Enter Stock Amount:"))
sum=sum+stock
counter=counter+1
avg=sum/counter
csum=csum+avg
ccount=ccount+1
print("The Average for",clientname,"is:",avg)
elif determiner==str('No' or 'no' or 'NO'):
cavg=csum/ccount
print("The average for all the clients is:",cavg)

How do i filter wrong inputs in python> [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed last year.
Here is my code so far - I am simply trying to say that if the input isnt one of these words, to redo the loop. However, although 'monday' works, the other days dont.
run_day = input("On which day do you want your code to run? ")
while run_day.lower() != ("monday" or "tuesday" or "wednesday" or "thursday" or "friday" or "saturday" or "sunday"):
run_day = input("On which day do you want your code to run? ")

Why "`trade".replace("`", "") is "trade" resulting False and "".join(list("`trade")[1:]) is "trade" also False [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 1 year ago.
I wondering why the following tests return False:
Suppose I have 2 simple strings:
str0 = "trade"
str1 = "`trade"
I don't understand why the following tests in python return False:
str1.replace("`", "") is str0
And,
"".join(list(str1)[1:]) is str0 => False
Thanks for your education!
In Python, is compares two objects in memory, == compares their values. Since your two variables are stored at two different places in memory, your comparison using is evaluates to false.

Python While Loop, Confused on "or" operator [duplicate]

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 4 months ago.
Basically I have a while loop that I want to run until either userWins or botWins equal 5. But with this code it will only stop when the variable "userWins" equals 5. I'm newer to python so I don't know if there is a different operator I am supposed to use to make this happen. Thank you for your help.
while userWins < 5 or botWins < 5:
So you want to run while both userWins and botWins are lower than 5.
You should use while userWins < 5 and botWins < 5: that's not a programming problem but a logic problem.

Using or in if statement (Python) [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 5 years ago.
I'm just writing a simple if statement. The second line only evaluates to true if the user types "Good!"
If "Great!" is typed it'll execute the else statement. Can I not use or like this? Do I need logical or?
weather = input("How's the weather? ")
if weather == "Good!" or "Great!":
print("Glad to hear!")
else:
print("That's too bad!")
You can't use it like that. The or operator must have two boolean operands. You have a boolean and a string. You can write
weather == "Good!" or weather == "Great!":
or
weather in ("Good!", "Great!"):
What you have written is parsed as
(weather == "Good") or ("Great")
In the case of python, non-empty strings always evaluate to True, so this condition will always be true.

Categories

Resources