0
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?

2
  • 3
    You need to rewrite the condition as either a == 'Allahyar' or a == 'allahyar' or a in ['Allahyar', 'allahyar']. Or even simpler a.lower() == 'allahyar'. This is because how conditions are evaluated in your expression. Commented Mar 28, 2022 at 6:25
  • oh thanks so much I dont see how i missed this part 𝐚 == '𝐀𝐥𝐥𝐚𝐡𝐲𝐚𝐫' 𝐨𝐫 𝐚 == '𝐚𝐥𝐥𝐚𝐡𝐲𝐚𝐫' Commented Mar 28, 2022 at 6:29

3 Answers 3

1

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"]:
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

awesome thanks for the timely reply.. Im super new to stackoverflow and coding in general and my god this place is freakin awesome.. super fast replies with detailed explanations.. i love you guys :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.