Home > Article > Backend Development > How to enter a number from the keyboard in Python
Get numbers from the keyboard: 1. Import the input module; 2. Use the input() function to prompt the user for input and return a string; 3. Convert the string to a number, such as using the int() function.
How to enter a number from the keyboard
Entering a number from the keyboard is a simple and common method in Python operate. Here's how to do it:
Use Python's built-in input
module to get keyboard input. First, import the module using the import
statement:
<code class="python">import input</code>
input
Function is used to read user input from the keyboard and return it as a string. To enter a number, specify a prompt message within parentheses that prompts the user for input:
<code class="python">num = input("请输入一个数字:")</code>
from input
Strings returned by functions must be converted to numbers in order to perform mathematical operations. Convert a string to an integer using the int()
function:
<code class="python">num = int(num)</code>
Now the variable num
will store the number entered by the user which can be used for mathematical operations or further deal with.
The above is the detailed content of How to enter a number from the keyboard in Python. For more information, please follow other related articles on the PHP Chinese website!