mardi 20 mars 2018

What's the best way to allow a user to select from multiple functions to run in Python?

I've got 20 files, each defining their own function. Sets of files are related. Each file defines a sport and an age group. So, as an example, these functions exist, each in their own individual file:

footballAdults()
footballTeens()
footballKids()
tennisAdults()
tennisTeens()

.... etc.

I want to allow the user to to call any one of them through a main file, filtering their choices like so:

x = input("Pick a sport:")
y = input ("Pick an age group:")

Then, the program will filter through the user's choices to run the function the user has chosen.

Is there a better way to do this other than by doing a huge if...elif...elif...elif...else statement, or am I stuck doing that?

Example:

from footballAdults import footballAdults
from footballTeens import footballTeens
from footballKids import footballKids
from tennisAdults import tennisAdults
#this would continue on until all 20 files have had their functions imported

x = input("Pick a sport:")
y = input ("Pick an age group:")

if x == ("football") and y == ("adults")
    footballAdults()
elif x == ("football") and y == ("teens")
    footballTeens()
elif x == ("football") and y == ("kids")
    footballKids()
elif x == ("tennis") and y == ("adults")
    tennisAdults()
#this would continue on until the final else statement
#cycling through all 20 functions

Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire