Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python Minimalist tutorial (5) input and output

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Input function to receive keyboard input. It is mainly used to increase the fun of practice in the process of learning and practice. Let our program be relatively complete and have simple interactive ability.

The output function prints the results of the code on the console, which also allows us to observe the results of the program. It is also to increase the interactive ability of the practice program. In the future, in the actual coding process, if we encounter problems and need to debug, we can also use the output function to print out the intermediate value generated during the running of the code, which can help us better locate the problem.

Input function input ()

The function that receives input from the keyboard is input (). The name is followed by parentheses, which is called a function call. There must be parentheses, which are also used to receive parameters for the function.

When the input () function runs, it displays what is passed in parentheses, and then the cursor waits for user input at the end. After entering the content, the user needs to press the enter key to end the input.

> name = input ("Please enter your name:") # the parameters received in parentheses of the input function are used to print input prompts on the screen. Please enter your name: nemo # this is shown in the previous line of code running, and the cursor will wait for input after the colon > namenemo

Note that the input received from the keyboard is always a string type.

> num = input ("Please enter a number:") Please enter a number: 5 > num'5' # Note, there are quotation marks, indicating that it is a string > num + 5TypeError: must be str, not int # error, string and number cannot be added > type (num)

In addition, in many cases, you can use the input () function to interrupt the program:

Print ('executing...') input ('Please press enter to continue) # code execution here will wait for the user to press enter print (' continue execution') Output function print ()

The print () function is a very common function.

The prototype of the print function is print (* args, seq='', end='\ n') * args: used to receive any number of printed content sep: used to decide which separator to use to separate multiple printed content end: used to decide which Terminator to use

Let's start with a classic example:

> print ('Hello worldview') Hello world!

Print () can receive multiple items that need to be printed, separated by commas.

> print ('I', 'yes', 'Nemo') I am a Nemo with more than # printed content, which is separated by spaces by default

If you want to change the separated characters, you can modify the sep parameter.

> print ('I', 'yes', 'Nemo', sep=' |') # separate me with vertical bars | Yes | Nemo

Print () also has a default ending character that is the newline character\ n, which means it breaks every time it is printed. If you don't want to do so, you can change the end parameter.

> print ('I', 'yes', 'Nemo', sep=' |', end='*') I | Yes | Nemo*

Note that sep and end must be written at the end!

Thinking about this section

How do I handle the number received by the input () function so that it can add and subtract integers normally?

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report