+ 1
when is elif used in ?
5 Antworten
+ 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.
+ 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
+ 2
You might also have a look at this explanation by someone else >> https://code.sololearn.com/cGP4eDqk0TDP/#py
+ 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
0
It's substitutes for "else, if"