+ 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.

6th Feb 2019, 6:11 PM
Abhishek V
Abhishek V - avatar
4 Respostas
+ 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/
6th Feb 2019, 6:32 PM
giannismach
giannismach - avatar
+ 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.
6th Feb 2019, 8:18 PM
Charlie S
Charlie S - avatar
+ 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.
6th Feb 2019, 6:33 PM
Charlie S
Charlie S - avatar
0
Charlie S There is no switch in python though, as far as I know.
6th Feb 2019, 8:09 PM
Matthias
Matthias - avatar