Why do you need input?

Introductory tutorials, case source code, learning materials, readership

Please visit:  python666.cn

Hello everyone, welcome to Crossin's programming classroom!

Friends who know even a little bit of Python basics should know that there is a built-in input function in Python: input

The role of input

The function of this function is very simple, it is to get the user's input from the console.

However, it is simple and simple. For those who are new to programming, they are likely to get stuck at this step. Because it involves two concepts: what is a "console" and what is "user input".

The so-called console, also called command line, terminal, is an interface for interacting with computer systems or programs. It usually looks like white characters on a black background, with the current directory name at the beginning, followed by a cursor that keeps beating.

77213668d761b1cbe4e7980a94b5c567.jpeg

Different systems have different system consoles, and different Python development tools also have their own Python consoles. And in the system console, you can enter the Python console through the python command. So this is a bit confusing, causing people to often enter Python code in the system console, or execute system commands (such as pip) in the Python console, and then find that the expected effect cannot be obtained.

For input, it is not difficult to find the console, because you can use the print statement to output, or add prompts to the input (to be mentioned later), and see where the output is, and that is where we provide the input console.

As for "user input", it is something that can be understood after watching the demonstration video. But if you only explain it in words, many people will misunderstand it. For example, write the value you want to enter in the parentheses after input, or see that the program is stuck after running and does not know that it is waiting for your input at this time.

use input

1. When the program runs to the input statement, it will stop running and wait for user input.

a99782ca020f76af3b0daa055691353c.png

2. At this time, the user of the program needs to input content at the jumping cursor on the console, and then press Enter to confirm and complete the input.

86c6ed89945479012001f152c20a17c4.png

3. The content entered by the user will be obtained by the input function, and can be assigned to a variable to record for subsequent code use

ff39e101b73cf44d682e005a8f4a0bfb.png

This is the process of an input getting input.

hint

As for the content in the brackets after the input, that is the input "prompt".

You can give nothing, if you give it, it will be output on the console before waiting for input, which is equivalent to doing a print.

811bc8b3de4aef8d75a6131412886e00.png

Why do you need input?

Someone is about to ask:

What value is needed, just assign it to the variable directly, why use input?

Direct assignment means that the person who writes the code writes it in the code during development;

The input gets the user's input, and the value is known only after the program runs, and it can be different every time it runs.

For example, in the simplest "hello world" program, if an input is added to allow users to enter nicknames, each person can get different output results.

203edecfb97024757a47d2f7cd92cf64.png

In this way, the same code can satisfy different people.

software input

Why have I never seen this kind of console input in other software?

There are many ways to obtain user input. The more common ones are input boxes , buttons , and even microphones and cameras . Users are inputting information into the program.

Input via the console, probably only used by developers.

For now we are just learning from the simplest way. More human-friendly input can also be written in the future.

type of input

The values ​​obtained by input are all strings , even if you input a number or a calculation formula, it will treat it as a string.

If you want to perform calculations as numbers, you need to perform type conversion, otherwise an error will be reported.

2975d538d0c9fe077ebb535e7e4a252f.png

The problem is a bit different in Python 2, when there were two input functions: input and raw_input. Among them, what input gets is the value itself, for example, inputting a number means getting a number.

Fortunately, almost no one uses Python 2 to learn now, and almost no one steps on this often confusing pit.

What else do you want to know about typing in Python? Welcome to discuss in the message area.


The following is the video time, welcome everyone to pay attention, like, and forward:


Crossin's second book " Operation on Code: Using Python and ChatGPT to Efficiently Get Excel Data Analysis " is now on the market.

caad76bd3689d203b117958f90dea846.jpeg

This book explains the ideas, methods and practical applications of processing and analyzing data from the perspective of the combined use of Python and Excel. Whether you are a learner who wants to engage in data analysis or an office worker in other occupations, you can master the skills of Python to analyze data through the study of this book. The book innovatively introduces ChatGPT into teaching, uses ChatGPT to answer questions and provides practical training codes, and introduces some practical skills of using ChatGPT to assist learning, bringing learners a new way of learning.

Readers and friends of the official account can contact me in the background after purchase and join the reader exchange group. Crossin will open the accompanying reading mode for you and answer all your questions when reading this book.

Thank you for retweeting and liking ~


_Previous article recommendation_

Python variables, are they containers or labels?


If you want to learn about paid quality courses and teaching Q&A services

Please reply in Crossin's programming classroom : 666

9cd1ea92c728c53fd2f1eb984b53225d.jpeg

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/131908046