Use input() function in python to get user input
The function input() lets the program pause and waits for the user to input some text. After getting the user's input, Python stores it in a variable for later use.
name =input("Tell me your name,and I will repeat it back to you:")print(name)
The function input() receives a parameter, which is a prompt or description that the user wants to show, so that the user knows what to do. After the user enters and press the enter key, the next sentence will be executed,
The input by the user has been stored in the variable name. If you print name, the name entered by the user is printed.
It should be noted that input() stores a string. Even if the input is a number, Python will also store it as a string of numbers. Mathematical operations cannot be performed. If you want to perform mathematical operations,
You need to use the function int() to convert a string to a number type:
age =input("tell me your age? ")ifint(age)=18:print("age = 18")else:print("age < 18")
Knowledge point expansion:
Python reads keyboard input
raw_input function
The raw_input([prompt]) function reads a line from standard input and returns a string (with the trailing newline removed)
#! /usr/bin/python
str =raw_input("Enter your input: ");
print "Received input is : ", str
input function
The input([prompt]) function and raw_input([prompt]) function are basically interchangeable, but input will assume that your input is a valid Python expression and return the result of the operation.
#! /usr/bin/python
str =input("Enter your input: ");
print "Received input is : ", str
So far, this article on how python obtains input from the keyboard is introduced. For more related python how to obtain input from the keyboard, please search the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support more in the future ZaLou.Cn!
Recommended Posts