0

Let's say I have functions called intercept, Bobby, Jimmy, and Tom. I want the user to type "run intercept"

The names are profiles

I'm new to python so the way I'm approaching this is just

input()

The problem is if the user types "run intercept" it won't run the intercept function cause the user input is a string.

I also can't use if statements

userInput = input()
if userInput == "intercept":
    intercept()

because if the user writes "run jimmy" I need it to run. I could use an elif statement

userInput == input()
if userInput = "intercept":
    intercept()
elif userInput = "jimmy":
    jimmy()
else
    create_new_profile()

but the point is that if there isn't a profile under any name they will create a new profile and automatically add another function. This wouldn't work because then I would have to manually add an elif statement everytime I added a new profile

I need a way for the program to take understand the input is looking for a function, search a list of functions, and run the correct function.

Idk how primitive or idiot this sounds due to my lack of experience but I would be really grateful if you someone could help me

intercept
  • 55
  • 7
  • Possible duplicate of [Calling a function of a module from a string with the function's name in Python](http://stackoverflow.com/questions/3061/calling-a-function-of-a-module-from-a-string-with-the-functions-name-in-python) – M. Prokhorov Apr 23 '17 at 10:20

2 Answers2

1

It is quite risky to call unknown user input directly. The allowed function names can be put as keys in a dictionary pointing to the actual functions:

def intercept():
    print("Function intercept.")

user_functions = dict(
    intercept=intercept,
    # ...
)

user_input = input("Input function name: ")  # Python 3, raw_input for Python 2

if user_input in user_functions:
    user_functions[user_input]()
else:
    print("Error: Unknown function.")

Result:

$ python3 test.py
Input function name: intercept
Function intercept.

$ python3 test.py
Input function name: foobar
Error: Unknown function.

Functions can also be dynamically created, e.g.:

def intercept():
    print("Function intercept.")

def jimmy():
    print("Function Jimmy.")

user_functions = {
    'intercept': intercept,
    # ...
}

def create_new_profile(name):
    print("Creating profile for " + name)
    def profile_function():
        print("Profile " + name)

    user_functions[name] = profile_function


while 1:
    user_input = input("Input function name: ")

    if user_input in user_functions:
        user_functions[user_input]()
    else:
        create_new_profile(user_input)

    print()

Result:

$ python3 test.py
Input function name: intercept
Function intercept.

Input function name: Jimmy
Creating profile for Jimmy

Input function name: Alice
Creating profile for Alice

Input function name: Alice
Profile Alice

Input function name: Bob
Creating profile for Bob

Input function name: Bob
Profile Bob

Input function name: Alice
Profile Alice

I do not know, what the "profile functions" are supposed to do. Maybe, a data driven approach may be better. A dictionary maps user names to user data. If the user input name is not inside the profile dictionary, then the function create_new_profile is called that adds the new user. The following example implements the user data as class including the function that is run.

class UserProfile:
    def __init__(self, name):
        self.name = name

    def run(self):
        print("User: " + self.name)


def intercept():
    print("Function intercept.")

user_profiles = {
    'Jimmy' : UserProfile('Jimmy'),
}

def create_new_profile(name):
    print("Creating new profile for " + name)
    user_profiles[name] = UserProfile(name)

user_functions = {
    'intercept': intercept,
    # ...
}

while 1:
    user_input = input("Input function name: ")

    if user_input in user_functions:
        user_functions[user_input]()
    elif user_input in user_profiles:
        user_profiles[user_input].run()
    else:
        create_new_profile(user_input)

    print()
$ python3 test.py
Input function name: intercept
Function intercept.

Input function name: Jimmy
User: Jimmy

Input function name: Alice
Creating new profile for Alice

Input function name: Alice
User: Alice
Heiko Oberdiek
  • 1,193
  • 8
  • 10
  • Thanks, I just tested it and it works. If there is more than one profile (Jimmy, Tommy...), Is there any way to add another function if the function name they called didn't exist. Like it tells them to give a name, user gives a name, then it creates a function in that name – intercept Apr 23 '17 at 00:19
0

I hightly advise against this... but you can use eval. This is using python2.7, with python3.x you should be able to use input rather than raw_input:

def apple():
    print('Mmm apples.')


def banana():
    print('Oh, a banana.')


user_input = raw_input()
eval(user_input + '()')

And the results:

python run_options.py
banana
Oh, a banana.

python run_options.py
apple
Mmm apples.
Robert Seaman
  • 1,846
  • 10
  • 14