After learning how to write ' Hello World ', we are still not satisfied, because this code will be written dead, and later when the run will only print a fixed word.
However, I would like to learn how to use user interaction when I want to manually enter content after the program has run.
In python2.x, user interaction uses the raw_input (" hint content ") method, such as
Name = Raw_input (" Please enter name:")print name
The previous hand style is omitted, and the result of the code execution is:
Enter something and return.
Here is a note, that is, when the program is running to wait for the user to enter the place, will always wait , that is, the following code will not continue to execute , and even quit with CTRL + C force to quit.
In addition, the data entered here will be converted to a string type, note that sometimes the type error in the function arguments, do not ask me how to know.
Once provided in the first pass, in 3.x, raw_input () has been replaced by input (), and the character of input () is different:
Name = input (" Please enter name:")print type (name)print name
where the type () function is used to view data types, which can be used directly, is called built-in functions or inline methods.
When we entered the same as above, but found an error, about the error prompt how to see the summary, here the error means that the variable is not defined.
If I change the input mode:
When I enclose it in quotation marks, I find it is possible again. Here we can conclude that input () has the following characteristics: What type of data is entered and what type it is. This is very different from the raw_input (), please pay attention to it.
When I see the error after the first input, some people ask, how do I enter a variable name that is already defined (that is, already assigned), and is it possible to invoke the value in memory?
In 2.7 (because I only used 2.7, the other 2.x is not clear), it is possible, but not in 3.x, because this function is another built-in function eval ("name") instead of (the "name" is the passed-in string and then the passed-in string as the variable name).
Of course, the user interaction function of input () is still there, just can't call the variable.
Above, is the basic content of user interaction, there is something missing, I will continue to add.
User interaction in 4.python