young boss
young boss

Reputation: 19

How come my if else statement isn't executing properly

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?

Upvotes: 0

Views: 45

Answers (3)

young boss
young boss

Reputation: 19

as @pavel stated the correct code is 𝐚 == '𝐀𝐥𝐥𝐚𝐡𝐲𝐚𝐫' 𝐨𝐫 𝐚 == '𝐚𝐥𝐥𝐚𝐡𝐲𝐚𝐫' I just got confused so I made an elif statement but the actual code was this 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 a == 'allahyar':
    print(a+ ' is a boomer')
else:
    print(a+' has been sent to the pyramid realm')    

there is no reason for the elif statement its just extra code to write for no reason.

Upvotes: 0

NotAName
NotAName

Reputation: 4322

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.

Upvotes: 0

Python learner
Python learner

Reputation: 1145

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"]:

Upvotes: 1

Related Questions