"Learning Python with You Hand in Hand" 14-Interactive Input

In the previous article "Learning Python with You Hand in Hand" 13-Operations , we learned the types of operations in Python. So far, we have learned the related concepts of strings and numbers, and mastered a variety of operations.

If you follow the writing of other tutorials, we will continue to introduce the related concepts of lists, dictionaries, tuples, and sets. But such continuous and boring concepts will not only cause the previous content to be forgotten due to lack of application, but more importantly, it may make everyone lose the motivation and motivation to continue learning.

Therefore, I am going to adjust the order of learning content later, putting the introduction of the three control flow statements in Python first. In this way, everyone can use these control flow statements to connect the content we learned before and write a complete program, which not only reviews the original knowledge, but also makes everyone’s learning more fulfilling during the programming process. It is easier to persist.

In addition, from the beginning of this article, we can use the PyCharm we first introduced. Because this is the first time we use PyCharm to write a program (the Console used before can only be regarded as a small feature), we will show you the whole process including screenshots to help you learn how to use PyCharm as soon as possible.

Before introducing the control flow statement, we will learn a new function input() in this article to realize the information interaction with the computer.

I don’t know if you have noticed that we all directly wrote command statements before running, but this way of execution is only the computer's output process of our instructions, and no information input process. Although it can also achieve the purpose of letting the computer execute our instructions, it cannot be called a complete information flow execution process.

The truly complete information flow execution process should include three parts: information input, command execution, and result output. And input() is a function used by Python to receive information input and realize the interaction with the computer.

Give an example, let everyone experience how the complete information flow execution process is. Also introduce the whole process of using PyCharm to write a program and run it.

The first step, double-click the PyCharm application icon, the program runs and displays the following interface.

The second step, click Create New Project, the New Project dialog box appears. Enter the file save path in the dialog box, and click the radio button in front of Existing interpreter. Under normal circumstances, the path of the installed interpreter will be written by default. If not, you can select the installed interpreter by clicking the drop-down box or... at the back.

The third step, according to the figure below, right-click the folder name of the file save path (here the folder name is Python), move the mouse to New in the newly appeared menu, and click Python File.

The fourth step, in the dialog box that appears, enter the file name (here the file name is text).

The fifth step is to enter the program content in the same position as the figure below. After the input is complete, right-click in the blank area and click Run'text' in the menu bar (that is, the file name entered in the previous step).

In the sixth step, PyCharm will execute line by line according to the program content we just entered. When the first line is executed, we are asked to input content, and a prompt appears on the lower console (this prompt is the prompt content we wrote in the program). Click the input position with the mouse and input the content we need to print.

The seventh step, after entering any content, press Enter. The program will continue to run and execute the print() function to print out what we just entered.

The above is a complete process of writing and running programs using PyCharm. Among them, the first command executes the input() function, which is the process of information input, which realizes the information interaction between us and the computer. After that, by executing the print() function, the result is finally output.

Because it is the first time to use PyCharm with everyone, I wrote this process in more detail. In the subsequent demonstration process, if it is not particularly necessary, we will show you examples like the previous article. For the example just now, it will be shown as follows.

In [1]: string = input("请输入需要打印的内容: ")
        print(string)
Out[1]: 请输入需要打印的内容: 123
Out[2]: 123

Among them, the In part is what we need to input in one time. In the Out part, if there is input content, it will be written together, and then use the new Out number to display the output result after input, and so on, until the final running result.

If you have problems during the execution of the above program, it is most likely that you did not choose a good interpreter. At this time, just select the installed interpreter in the lower right corner of PyCharm, as shown in the following figure:

Finally, we return to the input() function.

The input() function is very simple, like the input("Please enter the content that needs to be printed:") we just entered, the string in () is called the prompt message, which allows us to better interact and let the program users Know what needs to be entered. If nothing is written in (), just input the interactive content directly in the corresponding place on the console. It just makes the user not know when and what to enter, so this method is not recommended.

The input() function defaults to continuous input after the prompt information is displayed. Just like the example above, it is directly input "123" after "please input what needs to be printed:". But sometimes this method is not particularly friendly to the inputter, so here are two small tips for everyone:

One is to put one or two spaces after the prompt information string, so that it will not appear particularly compact, and the prompt information and the input content can be distinguished in position. In this example, a space is placed after the ":" .

Second, after the prompt information is displayed, the user can input a new line. As for how to implement it, here is a question for everyone (the answer will be announced in the next article). We have introduced the specific method before, so you can recall it. If necessary, you can click on the article "Learning Python with You Hand in Hand" 6-String Identification to find the answer.

This article used a large length to introduce the use of PyCharm, and everyone also learned how to use the input() function to achieve information interaction with the computer. The next article will introduce the control flow judgment statement if, so stay tuned.

 

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

Welcome to scan the QR code below, follow the "Yesu Python" public account, read other articles in the "Learning Python with You Hand in Hand" series, or click the link below to go directly.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

"Learning Python with You Hand in Hand" 4-Hello World!

"Learning Python with You Hand in Hand" 5-Jupyter Notebook

"Learning Python with You Hand in Hand" 6-String Identification

"Learning Python with You Hand in Hand" 7-Index of Strings

"Learning Python with You Hand in Hand" 8-String Slicing

"Learning Python with You Hand in Hand" 9-String Operations

"Learning Python with You Hand in Hand" 10-String Functions

"Learning Python with You Hand in Hand" 11-Formatted Output of Strings

"Learning Python with You Hand in Hand" 12-Numbers

"Learning Python with You Hand in Hand" 13-Operation

For Fans: Follow the "also said Python" public account, reply "Hand 14", you can download the sample sentences used in this article for free.

Also talk about Python-a learning and sharing area for Python lovers

 

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/98849159