Home >Backend Development >Python Tutorial >How to use the input() function to obtain user input in Python 3.x
How to use the input() function to obtain user input in Python 3.x
Python is a simple and easy-to-learn programming language that provides powerful functions and a rich standard library. When writing Python code, you often need to get input data from the user. Python provides the input() function, which can be used to obtain user input.
The basic usage of the input() function is to pass in a string in parentheses as the prompt information, then wait for the user to enter data, and return the data entered by the user as a string. Here is a simple example:
name = input("请输入您的姓名:") print("您好," + name + "!欢迎使用 Python。")
In this example, when the code runs to the input() function, the program will pause and wait for user input. The content entered by the user will be saved in the variable name. The program then prints a welcome message in which the name variable is replaced with what the user entered.
It should be noted that the input() function returns a string. If the user needs to enter a numeric value, the int() or float() function can be used to convert the string to the corresponding numeric type. Here is an example:
age = int(input("请输入您的年龄:")) print("您的年龄是:" + str(age))
In this example, the string returned by the input() function is passed to the int() function to convert it to an integer type. The program then prints the age entered by the user.
When the user enters an illegal value, such as entering a non-numeric character, the int() or float() function will throw a ValueError exception. In order to avoid program crashes, you can use exception handling mechanisms in your code to catch and handle exceptions. Here is an example:
try: age = int(input("请输入您的年龄:")) print("您的年龄是:" + str(age)) except ValueError: print("输入的年龄无效,请重新输入一个整数。")
In this example, the code in the try block attempts to convert the user's input to an integer, and if the conversion fails, a ValueError exception is thrown. Inside the except block, we print out an error message using the print() function.
In addition to obtaining user input, the input() function can also accept an optional parameter as a prompt message. This prompt message will be displayed on the screen while waiting for user input. The following is an example:
age = int(input("请输入您的年龄:"))
In this example, while waiting for the user to enter their age, the prompt message "Please enter your age:" will be displayed on the screen.
When using the input() function to obtain user input, we can also combine other Python functions to achieve more complex functions. For example, we can use conditional statements to execute different blocks of code based on user input. Here is an example:
choice = input("请选择您的性别(输入 '男' 或 '女'):") if choice == "男": print("您选择的是男性。") elif choice == "女": print("您选择的是女性。") else: print("您的输入无效,请重新输入。")
In this example, based on the gender entered by the user, the program will print the corresponding information.
To summarize, the input() function in Python 3.x can easily obtain user input. We can use the input() function to obtain different types of input such as strings and values, and execute corresponding code based on user input. When handling user input, you need to pay attention to exception handling to avoid program crashes. By making reasonable use of the input() function, we can write highly interactive Python programs.
(Note: This article uses Python 3.x version as an example. The input() function behavior of Python 2.x version is slightly different. Readers can choose to use it according to the actual situation.)
The above is the detailed content of How to use the input() function to obtain user input in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!