+ 1
Can we use 2 else with 1 if statement ?
At times there are more than 1 conditions that we want to verify , can we use 2 else one after the other or there is a way around to it.
4 ответов
+ 2
You can use the elif statement.
ie.
x = 5
if x == 1: print('x is 1')
elif x == 5: print('x is 5')
else: print(x)
Here is the related lesson:
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2278/
+ 2
Matthias sorry then, I am more on C/C++ part. It's very confusing to use a language when you studied 3 others. I know python has its own syntax model, but didn't though and never observed that it doesn't have a switch function.
+ 1
There can't be 2 else one aftet another, because each needs a different condition. You can't write else(true): ...
For multiple conditions exists switch with cases and default, if you want by the second else a default value. Otherwise use elif or else if.