Open In App

Using User Input to Call Functions - Python

Last Updated : 16 Dec, 2024
Comments
Improve
Suggest changes
1 Like
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:

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.

Explanation:

  • This code perform respective operations.
  • Maps input to functions.
  • It calls the function based on input .

Article Tags :
Practice Tags :

Similar Reads