Working with user inputs is a crucial aspect of programming, as it allows you to interact with and receive information from the user.
Being able to accept user inputs can make your programs more interactive, dynamic, and user-friendly. In Python, accepting user inputs is quite simple using the input()
function. In this tutorial, you will learn how to add user inputs in Python and use them in your programs.
Step 1: Using the input() function
The Python input()
function is a built-in function that reads a line from input and returns it as a string. Here’s an example of how it works:
1 |
user_input = input() |
By default, the input function will return the user-entered value as a string, so if you’re expecting a number or other input type, you’ll need to convert it.
Step 2: Prompting the user with text
You can provide a string argument inside the input()
function to display a message to the user, prompting them to enter a specific value. For example, if you want to ask the user for their name, you can do this:
1 2 |
name = input("Please enter your name: ") print("Hello, " + name) |
After executing this code, the user will see the message “Please enter your name:” and will be able to enter their name. The input value will be stored in the name variable, and the program will print “Hello, [user’s name]”.
Step 3: Converting user input to different data types
As mentioned earlier, the input()
function returns the user’s input as a string by default. To work with numerical values, you will need to convert the input string into a number. You can use the int()
function for integers and the float()
function for floating-point numbers.
Here’s an example that gets two integers from the user, converts them to integers, and prints their sum:
1 2 3 4 |
num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) sum = num1 + num2 print("The sum of the numbers is:", sum) |
If the user inputs non-numeric values in this example, the program will throw a ValueError exception. You can use exception handling to prevent errors and ensure your program runs smoothly.
Step 4: Handling exceptions in user input
To handle exceptions in user input, you can use the try
and except
statements. Here’s an example that demonstrates how to handle non-numeric values when asking the user to input integers:
1 2 3 4 5 6 7 |
try: num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) sum = num1 + num2 print("The sum of the numbers is:", sum) except ValueError: print("Invalid input. Please enter numeric values.") |
In this example, if the user enters a non-numeric value, the program will catch the ValueError exception and print a message informing the user that their input is invalid.
Conclusion
Adding user inputs in Python is both simple and effective thanks to the input()
function. In this tutorial, you have learned how to get user input in Python, convert it to different data types, display a prompt for the user, and handle exceptions related to user input. Now, you can create more interactive and user-friendly programs in Python by properly managing user inputs.