Lesson 2, using python while loop

introduction:

  Last time learning the basics of using python turtle library, forward and backward, and steering. The lessons need to draw more graphics, simple things become simple multiply the. 0/1 is simple, but it can make up the colorful multimedia world.

Course content:

  1. Look python turtle library

  2. Draw a square, draw two squares, square painting more

  3. Videos axes, the plane rectangular coordinate system Videos

Thanks to photo-sharing network:

Look python turtle library:

  By import library, draw a circle, and the output of the sentence: "really not Xiangman, in fact, I am handsome !!!"

. 1 >>> Import turtle T AS # when it is introduced into the library turtle rename T 
2 >>> t.circle (80 )
 . 3 >>> t.forward (100 )
 . 4 >>> t.write ( " real Xiangman not, in fact, I was a guy !!! " )

 

The following into the theme, 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 of the same center of gravity and a pitch of 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)

 

  

 

 

   Figure is very beautiful, but did not meet the same requirements of our center of gravity, the main problem is the unfinished square direction of the arrow did not return to the original direction.

Painting a bunch of squares:

  We need to introduce the while loop, format:

  """

  while conditions:

    Content loop

  """

  Among them, that condition is satisfied, on the implementation of the contents of the cycle, pay attention semicolon, indicating the following is the main content. Content to be a cycle of need to press the Tab key to indent, python jobs this will help us intelligent indentation

  Specific examples:

    while 0 != 9: 

    while 0 > 9:

    while 9:

    while 9 + 9:

    a = 0

    while !a:

    a = 9

    while !a:

  The method of painting a bunch of squares, diverse, draw 10 experience:

 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  the while   10 :
 . 6      A = A + 1'd
 . 7      IF a> 10 : # in case of a> 10 is out of the loop
 . 8          BREAK 
. 9      t.fd ( 20 is + A * 60 )
 10      t.left (90 )
 . 11      t.fd (60 + 20 is A * )
 12 is      t.left (90 )
 13 is      t.fd (60 + 20 is A * )
 14      t.lt (90 )
 15      t.fd (60 + 20 is A * )
 16      t.lt (90)
17 
18     t.penup()
19     t.goto(-a*10,-a*10)
20     t.pendown()

  Found: the while loop itself with determining function

Number of paintings axis:

  Requirements, reaches -300 negative side, the positive terminal 300 arrive. Each hit a 20-pixel mark

 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) #画一个箭头
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 of axes drawn on 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  # Videos x-axis 
12 is a = -300
 13 is  the while a <= 300 :
 14      T. Goto (a, 0)
 15      T. Goto (a, -3 ) # Videos numerals 
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) #画一个箭头
26 
27 #画y轴
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 
36t.hideturtle () # Hide Arrow

  

Modified Optimization:

  Videos by using a cyclic coordinate axes, but the advantage of simple, can be added to expand, knowledge XY table, numerals

 

Course Review:

  Learning to program, you want to truly master program, exercise is essential for any quick success are little gain, the results can only be obtained Sidongfeidong. By this lesson, practice example familiar with python while loop,

  With particular attention to the various forms while cycling conditions. Conditions of the operation result is non-zero, the cycle can be performed.  

 

 

 

 

Guess you like

Origin www.cnblogs.com/freesiyi/p/11516768.html