when is elif used in ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

when is elif used in ?

python

23rd Apr 2017, 4:00 PM
Paparawsy Leeplux
Paparawsy Leeplux - avatar
5 Answers
+ 6
elif is used to check another condition, if the preceding one (whether being if or another elif) is False. Example: a = input('Enter your name:') if a == 'Sam': print('Hello Sam!') elif a == 'Dora': print('Hi Dora!') elif len(a) < 1: print('Welcome, oh Anonymous!') else: print('What an unusual name!') So it first checks if the name is 'Sam' and *stops* further checkups, if it is. Only if the name is not 'Sam', it will check other conditions - one after another. They of course do not have to be about the 'a' variable, they can be about anything as long as it can be either True or False. BTW, else will only be checked if *all* earlier checkups are False.
23rd Apr 2017, 7:22 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
Do you mean "Can you give examples of when an elif statement is used?" It is not very well explained on this site - have a look at this image search for pseudocode examples and see if that helps >>https://www.google.ie/search?newwindow=1&rlz=1CASMAE_enIE683IE683&biw=1093&bih=614&tbm=isch&sa=1&q=%22else+if%22+pseudocode&oq=%22else+if%22+pseudocode
23rd Apr 2017, 7:14 PM
Lium Goon
Lium Goon - avatar
+ 2
You might also have a look at this explanation by someone else >> https://code.sololearn.com/cGP4eDqk0TDP/#py
23rd Apr 2017, 7:16 PM
Lium Goon
Lium Goon - avatar
+ 1
(here's my attempt at a short/simple definition) if/else = This, OR if-not then That if/elsif/elsif/else = This, OR if-not then This, OR if-not then This, OR finally if-none-of-the above then This
23rd Apr 2017, 8:55 PM
Lium Goon
Lium Goon - avatar
0
It's substitutes for "else, if"
23rd Apr 2017, 6:39 PM
Brian