0

I have like 15 if statements in python:


if d == 1:
    do something

elif d == 2:
    do something

this for about 15 if statements

is there a better way to do this? Or is it the best way? I am beginning to learn that redundant code is bad, but I can't think of another way to do this.

Thanks!

ligma
  • 9
  • 2
  • Depends on what the "do somethings" are. One trick is to define a dict of separate functions, with the values `d` could take on as keys: `functions = {1: handle_1, 2: handle_2, ...}`, then `functions[d]()`. – chepner Jan 23 '23 at 17:26

0 Answers0