1

I'm trying to make a Python script that lets me choose what function to do (for example, clean the downloads folder or download videos). All of these cases have a separate script which I import into the main .py file. The way I currently have it set up, I get the input and then if it's 1 then execute the first function, if it's 2 execute the second function and so on. Here's my code currently:

    if action == 1:
        instagram_downloader()
        print("Done")
    elif action == 2:
        clean_downloads()
        print("Done")

While this isn't too bad for a small amount of functions, once I get into double digits it will get messy and inefficient. Is there a better and more efficient way to do this?

1 Answers1

0

Yes, use a dict. That's Python's equivalent of a case/switch-statement in other languages.

functions = {1: instagram_downloader, 2: clean_downloads}
function = functions[action]
function()
wjandrea
  • 28,235
  • 9
  • 60
  • 81