0
when is elif used in ?
4 Respostas
+ 8
If you want to do something under a certain condition, you use an if statement. If you want something else to run instead of that condition, BUT NOT ALWAYS you can use elif; or (also called an) else if. elif allows you to check for multiple statements, without having each statement to run the same block of code. You can also have as many elif's as you'd like.
# start
if condition1: # is this true?
statement(s) # if yes, do this
elif condition2: # hm.. above was false, is this true?
statement(s) # if yes, do this
elif condition3: # hm.. above was false, is this true?
statement(s) # if yes, do this
else: # everything above is false, just do this
statement(s)
# done
Note* If the conditions are true, the below elif's would not have been evaluated. The program would just do the statement(s), then skip the elif/else's.
+ 8
Whichever one is true first, from top to bottom. If nothing is true, the else statement would run.
If condition1 is true in my example, then nothing else below it would run (except the statement(s) given in condition1). the elif will only be evalutated if the 'if' or 'elif' statement above it is false.
I suggest you give it a try with real code. Once you try it with a bunch of variables you'll get the idea.
0
so what decide to give the right to do that condition instead of another ? is there numbers you can give like ranks ?
0
ok thanks.