I am writing a program in python that responds to about three different messages based on content in an if: elif: else:
format. I plan to expand this script to numerous (possibly over 100?) different options to which it will respond accordingly. A continual if: elif: elif: elif: elif: ... else:
seems like very poor coding to me, and maybe I should use a switch statement? Wondering what the community thinks is the best way to go about it. Maybe theres a better way I'm not thinking about.
Asked
Active
Viewed 12 times
1

SuperAdmin
- 538
- 2
- 7
- 20
-
Why don't you use a dictionary that stores the messages? Then you can have a single statement to do all of this. – Matt Jul 11 '15 at 22:02
-
@Matt Duh, of course. This is why I put the question to the community. My mind gets stuck in one train of thought and I miss the obvious. – SuperAdmin Jul 11 '15 at 22:03
-
You can even store functions inside of a dictionary so sometimes I'll build a big dictionary of function's and use the keys to make the call. That avoids the long switch statements, but it does make assumptions about the arguments you pass into the functions. – Matt Jul 11 '15 at 22:04