0

Python beginner: I would like to take input from the user, and based on the input, multiple functions will be executed:

def func1():
    'This is function 1'
def func2():
    'This is function 2'
def func3():
    'This is function 3'

funcDict = {'A': func1, 'B': func2, 'C':func3}

response = raw_input()

So if the user inputs the string 'A,B' , func1 and func2 will be executed

2 Answers 2

1

You can use your response to check whether that value is in the response, you can do this by checkig whether A, B, or C is in the string using if statements.

If "A" in response:
    func1()
If "B" in response:
    func2()
If "C" in response:
    func3()
Sign up to request clarification or add additional context in comments.

Comments

0

There are few ways this could be done. The first step would be to validate the user input. This could be done by checking if all the letters the user inputs are in the funcDict.

commands = response.split(",") # Split string on "," and return a list
valid = all([i in functDict for i in commands]) # check all commands are in funcDict

Then you could iterate of the commands and execute the corresponding function.

for command in commands:
    funcDict[command]()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.