Using User Input to Call Functions - Python Last Updated : 16 Dec, 2024 Comments Improve Suggest changes Like Article Like Report input() function allows dynamic interaction with the program. This input can then be used to call specific functions based on the user's choice .Let’s take a simple example to call function based on user's input .Example: Python def add(x, y): return x + y # Add def sub(x, y): return x - y # Subtract a = input() # Input if a == "add": res = add(5, 3) elif a == "sub": res = sub(5, 3) else: res = "Invalid operation" # Error print("Result:", res) Explanation:This code takes user input to determine the desired operation .Based on the input, the corresponding function is called.It outputs the result of the operation or an error message if the input is invalid.Using Dictionary for Function Mapping Instead of using if statements, we can use a dictionary to map user inputs to function calls. Python def multiply(x, y): return x * y # Multiply def divide(x, y): return x / y # Divide fun = { "mul": multiply, "div": divide } a = input() # Input if a in fun: result = fun[a](6, 3) # Call else: result = "Invalid operation" # Error print("Result:", result) # Output Explanation:This code perform respective operations.Maps input to functions.It calls the function based on input . Comment More infoAdvertise with us Next Article Using User Input to Call Functions - Python V vishakshx339 Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads How to Call Multiple Functions in Python In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a 3 min read How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de 5 min read Call a function by a String name - Python In this article, we will see how to call a function of a module by using its name (a string) in Python. Basically, we use a function of any module as a string, let's say, we want to use randint() function of a random module, which takes 2 parameters [Start, End] and generates a random value between 3 min read Python 3 - input() function In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string.Python input() Function SyntaxSyntax: input(prompt)Parameter:Prompt: (optional 3 min read How to use Function Decorators in Python ? In Python, a function can be passed as a parameter to another function (a function can also return another function). we can define a function inside another function. In this article, you will learn How to use Function Decorators in Python. Passing Function as ParametersIn Python, you can pass a fu 3 min read Like