Python3 chapter 4, python3
1. Output
Use print () to add a string to the brackets to output the specified text to the screen.
print('hello, world')
Enter the above Code and output it after execution:
hello, world
In fact, any basic type can be output using the print () function. By default, the output is a line feed. If you do not need a line feed, you must add end = "" at the end of the variable "":
X = "a" y = "B" # print (x) print (y) print ('--------- ') # print (x, end = "") print (y, end = "") print ()
Run the above Code and output:
ab---------a b
The print () function can also accept multiple strings separated by commas (,) to form an output string:
print('my','name','is','roy')
Run the above Code and output:
my name is roy
You should have discovered that the print () function will print each string in sequence. When the comma "," is used, a space is output.
In fact, there are three output methods:
- Print () function
- Expression statement
- Write () of the object ()
As a beginner, you only need to remember this method of the print () function.
With the output, let's do one thing: output the 9-9 Table in elementary school on the screen
print('1 x 1 = 1')print('1 x 2 = 2','2 x 2 = 4')print('1 x 3 = 3','2 x 3 = 6','3 x 3 = 9')print('1 x 4 = 4','2 x 4 = 8','3 x 4 =12','4 x 4 =16')print('1 x 5 = 5','2 x 5 =10','3 x 5 =15','4 x 5 =20','5 x 5 =25')print('1 x 6 = 6','2 x 6 =12','3 x 6 =18','4 x 6 =24','5 x 6 =30','6 x 6 =36')print('1 x 7 = 7','2 x 7 =14','3 x 7 =21','4 x 7 =28','5 x 7 =35','6 x 7 =42','7 x 7 =49')print('1 x 8 = 8','2 x 8 =16','3 x 8 =24','4 x 8 =32','5 x 8 =40','6 x 8 =48','7 x 8 =56','8 x 8 =64')print('1 x 9 = 9','2 x 9 =18','3 x 9 =27','4 x 9 =36','5 x 9 =45','6 x 9 =54','7 x 9 =63','8 x 9 =72','9 x 9 =81')
Run the above Code and output:
1 x 1 = 11 x 2 = 2 2 x 2 = 41 x 3 = 3 2 x 3 = 6 3 x 3 = 91 x 4 = 4 2 x 4 = 8 3 x 4 =12 4 x 4 =161 x 5 = 5 2 x 5 =10 3 x 5 =15 4 x 5 =20 5 x 5 =251 x 6 = 6 2 x 6 =12 3 x 6 =18 4 x 6 =24 5 x 6 =30 6 x 6 =361 x 7 = 7 2 x 7 =14 3 x 7 =21 4 x 7 =28 5 x 7 =35 6 x 7 =42 7 x 7 =491 x 8 = 8 2 x 8 =16 3 x 8 =24 4 x 8 =32 5 x 8 =40 6 x 8 =48 7 x 8 =56 8 x 8 =641 x 9 = 9 2 x 9 =18 3 x 9 =27 4 x 9 =36 5 x 9 =45 6 x 9 =54 7 x 9 =63 8 x 9 =72 9 x 9 =81
You may have discovered that we have output the residual table, but this writing method is too rigid and your hands are sore. Is there any elegant way? The answer is yes. As for how to do this, please leave it in the next two chapters.
Thinking: using the knowledge above in this chapter, there are no other ways to output the 9-9 residual table. Please write the code.
2. Input
Execute the following program and press enter to wait for user input:
Input ("\ n press enter and exit. ")
In the above Code, "\ n" will output two new blank lines before the result is output. Once you press the key, the program will exit.
You can input strings and put them in a variable. For example, enter the User Name:
Name = input ()
Print ('your name: ', name)
Run the above Code and you will be asked to enter it. Press enter to end the input. Assume that I enter
roy
Then the program will output:
Your name: roy
We can also give users a prompt when requesting user input. Change the code:
Name = input ("enter your name:") print ('your name: ', name)
Execution, output:
Enter your name:
Enter roy, press enter, and output:
Your name: roy
Thinking: using the knowledge learned in this chapter, "When we ask users to input, give users a prompt", how can we implement it? Please write your code