What is the correct syntax for 'else if'?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC I Beethoven Sonata No 31 in A Flat M
--
Chapters
00:00 Question
00:44 Accepted answer (Score 454)
01:18 Answer 2 (Score 21)
01:29 Answer 3 (Score 13)
01:40 Answer 4 (Score 10)
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/2395...
Answer 1 links:
[elif]: http://docs.python.org/3.1/tutorial/cont...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x
#avk47
--
Track title: CC I Beethoven Sonata No 31 in A Flat M
--
Chapters
00:00 Question
00:44 Accepted answer (Score 454)
01:18 Answer 2 (Score 21)
01:29 Answer 3 (Score 13)
01:40 Answer 4 (Score 10)
02:22 Thank you
--
Full question
https://stackoverflow.com/questions/2395...
Answer 1 links:
[elif]: http://docs.python.org/3.1/tutorial/cont...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x
#avk47
ACCEPTED ANSWER
Score 460
In python "else if" is spelled "elif".
Also, you need a colon after the elif and the else.
Simple answer to a simple question. I had the same problem, when I first started (in the last couple of weeks).
So your code should read:
def function(a):
if a == '1':
print('1a')
elif a == '2':
print('2a')
else:
print('3a')
function(input('input:'))
ANSWER 2
Score 21
Do you mean elif?
ANSWER 3
Score 13
def function(a):
if a == '1':
print ('1a')
elif a == '2':
print ('2a')
else:
print ('3a')
ANSWER 4
Score 10
since olden times, the correct syntax for if/else if in Python is elif. By the way, you can use dictionary if you have alot of if/else.eg
d={"1":"1a","2":"2a"}
if not a in d: print("3a")
else: print (d[a])
For msw, example of executing functions using dictionary.
def print_one(arg=None):
print "one"
def print_two(num):
print "two %s" % num
execfunctions = { 1 : (print_one, ['**arg'] ) , 2 : (print_two , ['**arg'] )}
try:
execfunctions[1][0]()
except KeyError,e:
print "Invalid option: ",e
try:
execfunctions[2][0]("test")
except KeyError,e:
print "Invalid option: ",e
else:
sys.exit()