# Lesson 2, Use of the python while loop

Introduction:

Last lesson, I learned the basic use of python turtle library, moving forward and backward. This lesson needs to draw more than one figure, so it is not easy to draw more simple things. 0/1 is simple, but it can make up a rich and colorful multimedia world.

Course content:

1. Look at the python turtle library again

2. Draw a square, two squares and multiple squares.

3. Draw the number axis and the plane rectangular coordinate system.

Thank you for sharing pictures on the Internet: Look at the python turtle library again:

By importing the library, drawing a circle, and output a sentence: "I'm really handsome!!!"

```1 >>> import turtle as t #Importing turtle Rename it as a library t
2 >>> t.circle(80)
3 >>> t.forward(100)
4 >>> t.write("To tell you the truth, in fact, I am a handsome man!!!")``` Let's move on to the theme and draw a square:

```1 import turtle as t
2 t.fd(100)
3 t.left(90)
4 t.fd(100)
5 t.left(90)
6 t.fd(100)
7 t.lt(90)
8 t.fd(100)```

Draw two squares with the same center of gravity, spacing 10 pixels:

- Error demonstration:

``` 1 import turtle as t
2 t.fd(100)
3 t.left(90)
4 t.fd(100)
5 t.left(90)
6 t.fd(100)
7 t.lt(90)
8 t.fd(100)
9 #t.lt(90)
10
11 t.penup()
12 t.goto(-10,-10)
13 t.pendown()
14
15 t.fd(120)
16 t.left(90)
17 t.fd(120)
18 t.left(90)
19 t.fd(120)
20 t.lt(90)
21 t.fd(120)
22 #t.lt(90)``` The picture is beautiful, but it doesn't satisfy our requirement of the same center of gravity. The main reason for the problem is that the direction of arrows does not return to the original direction after drawing the square.

Draw a bunch of squares:

We need to introduce our while loop in format:

"""

The while condition:

The content of the cycle

"""

Among them, if the condition is established, we should pay attention to the semicolon of the content of the execution cycle, and explain the following is the main content. The content to be recycled needs to be indented by pressing a Tab key, and python's homework will also be indented intelligently.

Specific examples:

while 0 != 9:

while 0 > 9:

while 9:

while 9 + 9:

a = 0

while !a:

a = 9

while !a:

There are many ways to draw a bunch of squares. Draw 10 pieces and experience them.

``` 1 import turtle as t
2
3 t.speed(100)
4 a = 0
5 while a < 10:
6     a = a +1
7     t.fd(60 + a*20)
8     t.left(90)
9     t.fd(60 + a*20)
10     t.left(90)
11     t.fd(60 + a*20)
12     t.lt(90)
13     t.fd(60 + a*20)
14     t.lt(90)
15
16     t.penup()
17     t.goto(-a*10,-a*10)
18     t.pendown()``` ``` 1 import turtle as t
2
3 t.speed(100)
4 a = 0
5 while  10:
6     a = a +1
7     if a > 10:  #Jump out of the loop in a > 10
8         break
9     t.fd(60 + a*20)
10     t.left(90)
11     t.fd(60 + a*20)
12     t.left(90)
13     t.fd(60 + a*20)
14     t.lt(90)
15     t.fd(60 + a*20)
16     t.lt(90)
17
18     t.penup()
19     t.goto(-a*10,-a*10)
20     t.pendown()```

It is found that the while loop itself has the function of judgment.

Draw the number axis:

Negative end reaches - 300 and positive end reaches 300. Mark each 20 pixels

``` 1 import turtle as t
2
3 t.speed(100)
4 t.write("0")
5 t.goto(-300,0)
6 t.goto(300,0)
7
8 t.lt(90)
9 t.circle(6,steps=3) #Draw an arrow
10
11 a = -300
12 while a <= 300:
13     t.goto(a,0)
14     t.goto(a,-3)
15     t.goto(a,3)
16     t.goto(a,0)
17     a = a + 20``` On the basis of the number axis, draw a plane rectangular coordinate system:

``` 1 import turtle as t
2
3 t.speed(100)
4 t.write("0")
5 t.goto(-300,0)
6 t.goto(300,0)
7
8 t.lt(90)
9 t.circle(6,steps=3) #Draw an arrow
10
11 #painting x axis
12 a = -300
13 while a <= 300:
14     t.goto(a,0)
15     t.goto(a,-3)#Drawing mark
16     t.goto(a,3)
17     t.goto(a,0)
18     a = a + 20
19
20 t.penup()
21 t.goto(0,-300)
22 t.pendown()
23 t.goto(0,300)
24 t.lt(90)
25 t.circle(6,steps=3) #Draw an arrow
26
27 #painting y axis
28 a = -300
29 while a <= 300:
30     t.goto(0,a)
31     t.goto(-3,a)
32     t.goto(3,a)
33     t.goto(0,a)
34     a = a + 20
35
36 t.hideturtle() #Hidden arrows``` Modification optimization:

The coordinate axes are drawn by using circles, but the advantages are simple and simple. Extension can be added, xy recognition, digital representation.

Course review:

Practice is indispensable for learning programming and mastering it. Any quick success and instant benefit can only bring about seemingly unintelligible results. Through practical examples, we are familiar with the use of python while loop.

Special attention should be paid to the various forms of while cycle conditions. If the result of conditional operation is non-zero, the loop can be executed. Keywords: Python Programming

Added by Spaceboy on Fri, 13 Sep 2019 09:53:13 +0300