Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am confused between 'else' and 'else if' or 'elif' in python. I do understand that an else does not need a condition whereas an else if does need a condition as it still an if clause. Is there any other difference between these two clauses and in the way we use them? Any sort of help would be highly appreciated. Thank you!
In languages like C, there was no elif keyword; there were only nested if-else statements. (I only use C as an example of not having elif, rather than claiming that no previous or contemporary languages did not.)
/* Pretend print is a defined function */
if (x == 1) {
print("one");
} else {
/* x != 1 */
if (x == 2) {
print("two");
} else {
print("many");
}
}
Because each body only needs to be a statement, not necessarily a compound statement, the bracket in the above can be dropped.
if (x == 1)
print("one");
else
if (x == 2)
print("two");
else
print("many");
and because indentation and newlines are just arbitrary whitespace, you can rewrite this to look almost like a single statement.
/* Still two statements */
if (x == 1)
print("one");
else if (x == 2)
print("two");
else
print("many");
Other languages took the step of "merging" else and if into a single keyword to allow conditional statements with more than one condition. python and POSIX shell uses elif. (perl is an example of language with a different keyword, elsif.)
The nested statement
if x == 1:
print("one")
else:
if x == 2:
print("two")
else:
print("many")
becomes
if x == 1:
print("one")
elif x == 2:
print("two")
else:
print("many")
TL;DR
else captures every other condition not captured by if or elif. elif acts like a ladder to capture extra but totally different conditions than if. There is no else if statement in python.
Elif (else-if) - lets you check for multiple expressions.
Else - Basically means if not true then...
Example
num = random.randint(1,2)
if num == 1:
print("The number is 1")
elif num == 2:
print("The number is 2")
else:
print("The number is neither 1 or 2")
Related
This question already has answers here:
Difference between multiple if's and elif's?
(9 answers)
Closed 1 year ago.
if is strong than elif in code?
or if and elif is same?
if a[number_counter] == answer:
a_count+=1
elif b[number_counter] == answer:
b_count+=1
elif c[number_counter] == answer:
c_count+=1
this code has problem than I change code to
if a[number_counter] == answer:
a_count+=1
if b[number_counter] == answer:
b_count+=1
if c[number_counter] == answer:
c_count+=1
than code not has problem.
It's different.
It basically else if but concated to elif. if you use elif you check the same condition and if one is met it exits.
If you use ifs the whole time it checks each if statement whether or not the previous one was true.
It's just normal Python Syntax.
x = 0
if x == 0:
#do something
#if True it doesn't check the rest
elif x ==1:
#do something
#if true if doesn't check the rest
elif x==2:
#do something
#if true doesn't check the rest
else:
#if none of these are True this gets exucuted
#do something
#It checks all these if statements whether or not one is true.
if x==1:
#do something
if x==2:
#something
This question already has answers here:
Putting an if-elif-else statement on one line?
(14 answers)
Closed 2 years ago.
I have the following Code in Python:
def show_sequence(n):
if n > 0:
return "+".join((str(i) for i in range(n+1))) + " = %d" %(sum(range(n+1)))
elif n == 0:
return "0=0"
else:
return str(n) + "<0"
Question: Is there a syntax correct way of putting all lines into one return statement if there are 3 if-statements?
I know it works with one if- & else-statement but im a fan of one-liner and already asked this myself several times.
Inline if-statements can be chained like this:
"a" if 0 else "b" if 0 else "c"
(replace the 0s with 1s to see the return value change)
You can use this:
result = True if a==b else False
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is what I've input:
def greater_less_equal_5(answer):
if 6 > 5:
return 1
elif 4 < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
and gave me this note
Oops, try again. It looks like your function output 1 instead of -1
when answer is 3. Make sure you filled in the if and elif statements
correctly!
and this is what came up in the upper right display:
>1
>1
>1
>None
No matter how I change around the numbers and the >/< I've even tried == and != it still outputs 1 1 1 None.
I've searched around for any possible tips and seen others stuck on the same problem as me and when I tried their solves I then get:
def greater_less_equal_5(answer):
if > 5:
return 1
elif < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
and the output is:
File "python", line 2
if > 5:
^
SyntaxError: invalid syntax
Is this test rigged to always output a failed result to make me pay for pro and ask for their help?
And the hint given for this is:
Make sure the if and elif statements end with colons :
Your code should look something like:
if EXPRESSION:
do something
elif OTHER EXPRESSION:
do something
else:
do something
Am I just missing something horribly basic?
You are indeed missing something basic - namely, that the output of your function doesn't depend on answer at all. No matter what you feed in as answer, because 6 > 5 is always True, it will always return the result of that case.
What you need is
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
elif answer == 5:
return 0
You're missing 'answer' variable for expression, which you pass into your function
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
Hello there I'm currently trying to get a good grasp of the if, elif, else structure in Python. I'm trying some weird combinations in python having a test program to know the output in this if, if, elif, elif, else code. However I'm getting weird results such as this
input = raw_input('Please enter the required digit: ')
intput = int(input)
if intput == 0:
print 'if1'
if intput == 1:
print 'if2'
elif intput == 0:
print 'elif1'
elif intput == 1:
print 'elif2'
else:
print 'else'
if I in put 1 it will print "if2", I thought that it will also print "elif2" and other shenanigans when I try to change the "intput == n" code. So my question is do I have to stick to the if,elif, elif, .... n * elifs, else method which seems to me working alright than working with the wacky if,if.... n * ifs, elif, elif, ...n* elifs, else.
Thanks
The elif tree is designed such that at anywhere along if one of the statement turns out to be True, the rest of the elifs will not be evaluated.
Here's a tutorial that might help you understand if else better.
this may be more clear to understand:
if input == 0:
print "if1"
switch(input):
case 1:
print "if2"
break
case 0:
print "elif1"
break
case 1:
print "elif2"
break
default:
print "else"
break
of course, the code does not work.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
the function i have written....
def calculate_price(Thickness, Width, Price, Timbmet):
foo = Thickness * Width;
bar = foo * Price;
if Timbmet == "n" or "N":
foobar = ((bar / 100) * 15) + 100
print foobar
else:
print bar
calculate_price(5., 4., 3., "y");
the console output is 109 which is wrong because it shouldn't have executed the code in the if statement because the Timbmet parameter does not match "n" or "N". What am I doing wrong and why is the code not running the else part?
Change condition to -
if Timbet in ["n", "N"]:
Programming does not work like natural language...
The condition Timbmet == "n" or "N" returns always True because the second subexpression "N" is always True. You need to either check if Timbet is in a list/set:
if Timbet in ("n", "N"):
if Timbet in {"n", "N"}: # even better but more confusing if we are not aware of the set construction syntax
... or check without the case sensitivity:
if Timbet.lower() == "n":