4. User Interaction in python; 4. python users

Source: Internet
Author: User

4. User Interaction in python; 4. python users

After learning how to write 'Hello world', we are still not satisfied, because the code will be completely written, and only a fixed set of words will be printed in the future.

However, after running the program, I want to manually enter the content and learn how to use user interaction.

In python2.x, The raw_input ("prompt content") method is used for user interaction. For example:

Name = raw_input ("Enter name:") print name

The previous starters are skipped, and the code execution result is:

Enter another item and press Enter.

Note that when the program runs to a place that is waiting for user input,Will keep waiting, That isThe following code will not continue to be executedTo exit, press ctrl + c to exit.

In addition, the input data is convertedStringType. Note that sometimes the type of parameter passing in the function is incorrect. Do not ask me how to know it.

 

It was provided in the first time. In 3. x, raw_input () has been replaced by input (), and the features of input () are different:

Name = input ("Enter name:") print type (name) print name

 

Specifically, the type () function is used to view the data type. This type can be directly used and is called a built-in function or a built-in method.

When we enter the same information as above, we will find that an error is reported. We will summarize the error message in the future.The variable is not defined.

  If I use another input method:

When I enclosed the quotation marks, I found it was okay again. Here we can conclude that the features of input () are:What type is input data?. This is very different from raw_input.

 

When an error is reported after the first input, some people will ask, How do I enter a variable name that has been defined (that is, assigned values, is it possible to call the value in the memory?

In 2.7 (because I only used 2.7, the other 2. x is not clear), but in 3. x does not work, because this function is replaced by another built-in function eval ("name") ("name" is the input string, and then the input string is used as the variable name ).

Of course, the user interaction function of input () is still available, but the variable cannot be called.

 

The above is the basic content of user interaction. If there is anything missing, I will continue to add it.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.