Home > Back-end >  How come my if else statement isn't executing properly
How come my if else statement isn't executing properly

Time:03-28

a = input("enter your first name ")

for i in range(len(a)):
    for space in range(len(a)-i):
        print(end=' '*len(a))
    for j in range(2*i 1):
        print(a,end = '')
    print()
print()

if a == 'Allahyar' or 'allahyar':
    print(a  ' is a boomer')
elif a != 'Allahyar' or 'allahyar':
    print(a ' has been sent to the pyramid realm')    
        

in this code the if statement executes no matter what and it completely ignores the elif statement. any idea why?

CodePudding user response:

Correct syntax to combine two conditions using logical or operator.

if var == "str1" or var == "str2":

You can also do it like this

if var in ["str1","str2"]:

CodePudding user response:

I'll expand on my comment. The reason you're getting the if condition always triggering is because you think that what you coded should equate to: "variable a is equal to either 'Allahyar' or 'allahyar'" but in reality it will get evaluated as two separate statements like so (brackets added for clarity):

(a == 'Allahyar') or (bool('allahyar'))

A non-empty string 'allahyar' will always evaluate to True so it's impossible for this combined expression to be False.

CodePudding user response:

as @pavel stated the correct code is

  • Related