+ 6

How to reduce if and elif?

If my code has lot of if and elif statements comparing lots of integers and strings then printing or performing any task so how can I reduce lots of if and elif? Thanks

16th Apr 2018, 3:30 PM
Aditya Rawat
10 Respostas
+ 6
cyk Thanks a lot!
16th Apr 2018, 6:01 PM
Aditya Rawat
+ 5
Jan Markus Thanks for the idea!
16th Apr 2018, 4:18 PM
Aditya Rawat
+ 5
cyk , Jan Markus I have added new problem I don't want to ask another Question so here is the problem 👇 I wanted to add a 'random' operator to so that it can give one of the answer, Thanks! https://code.sololearn.com/cLYqdqNvQPBA/?ref=app
16th Apr 2018, 5:43 PM
Aditya Rawat
+ 5
nice solutions guys!
17th Apr 2018, 5:05 AM
The Coder
The Coder - avatar
16th Apr 2018, 4:07 PM
Aditya Rawat
+ 4
This is what I came up with. If you do random_num = list then random_num no longer takes random values in the range(0, 3). It is a list now... So just keep it the way it is and for a random_num generated get the corresponding element in the list... I tested it entering hey several times and it kept giving me something random from the list https://code.sololearn.com/cLA2pd6boLpe/?ref=app
16th Apr 2018, 5:48 PM
cyk
cyk - avatar
+ 3
Well, maybe we can be able to suggest alternative ways of doing that if you posted part of the code
16th Apr 2018, 3:56 PM
cyk
cyk - avatar
+ 3
Jan Hahahaha... This is hilarious!
16th Apr 2018, 4:21 PM
cyk
cyk - avatar
+ 3
You can use the list within the If statement and random.choice to select one item. So the Code becomes shorter and more clear: https://code.sololearn.com/cS1Os6bOzdVA/?ref=app
16th Apr 2018, 7:42 PM
Sebastian Keßler
Sebastian Keßler - avatar