Taking user input is an essential aspect of programming, as it allows for a dynamic and interactive experience. In this tutorial, we will explore different methods to take user input in Python, which is one of the most widely used programming languages today.
Step 1: Using the input() Function
The simplest way to take user input in Python is by using the input() function. This function prompts the user to input a value, accept it as a string, and then return it.
1 2 |
name = input("Enter your name: ") print("Hello, " + name) |
In the code above, the user is asked to enter their name, which is then stored in the variable “name” and later used in the print statement.
Step 2: Converting the Input to the Desired Data Type
Since the input() function always returns a string, we need to convert the input to the desired data type in case we require integers or floating-point numbers. This can be accomplished using the int() and float() functions.
Here is an example that prompts a user for their age and calculates the number of years until they turn 100:
1 2 3 |
age = int(input("Enter your age: ")) years_until_100 = 100 - age print("You will turn 100 in", years_until_100, "years.") |
In this case, we used the int() function to convert the user’s input (age) into an integer.
To convert user input into a float, simply replace the int() function with the float() function in the code above:
1 2 |
height = float(input("Enter your height in meters: ")) print("Your height is:", height, "meters.") |
Step 3: Handling Multiple Inputs
We can use the input() function in conjunction with the split() function to handle multiple inputs in Python.
Let’s say we want the user to input three integers in a single line, separated by spaces:
1 2 |
a, b, c = map(int, input("Enter three integers separated by spaces: ").split()) print("You entered:", a, b, c) |
In the given example, the user inputs three integers separated by spaces, which are then split using the split() function and mapped to their corresponding variables using the map() function.
Full Code
The full code for the examples in this tutorial can be found below.
1 2 3 4 5 6 7 8 9 10 11 12 |
name = input("Enter your name: ") print("Hello, " + name) age = int(input("Enter your age: ")) years_until_100 = 100 - age print("You will turn 100 in", years_until_100, "years.") height = float(input("Enter your height in meters: ")) print("Your height is:", height, "meters.") a, b, c = map(int, input("Enter three integers separated by spaces: ").split()) print("You entered:", a, b, c) |
Output
Enter your name: John Doe Hello, John Doe Enter your age: 30 You will turn 100 in 70 years. Enter your height in meters: 1.75 Your height is: 1.75 meters. Enter three integers separated by spaces: 5 10 15 You entered: 5 10 15
Conclusion
By following the steps outlined in this tutorial, you can now easily incorporate user input in your Python projects, enabling more interactive and dynamic programs. Make sure to practice using the input(), int(), and float() functions along with the split() function to handle varying input scenarios.