when is elif used in ? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

when is elif used in ?

python

23rd Apr 2017, 4:00 PM
Paparawsy Leeplux
Paparawsy Leeplux - avatar
4 Réponses
+ 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.
23rd Apr 2017, 4:11 PM
Rrestoring faith
Rrestoring faith - avatar
+ 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.
23rd Apr 2017, 4:16 PM
Rrestoring faith
Rrestoring faith - avatar
0
so what decide to give the right to do that condition instead of another ? is there numbers you can give like ranks ?
23rd Apr 2017, 4:10 PM
Paparawsy Leeplux
Paparawsy Leeplux - avatar
0
ok thanks.
23rd Apr 2017, 4:14 PM
Paparawsy Leeplux
Paparawsy Leeplux - avatar