Lesson 2, Use of the python while loop

Time:2019-10-9

Introduction:

Last lesson, I learned the basic use of Python turtle library, 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 more than one square.

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 into the library, drawing a circle, and output a sentence: “I’m really handsome!!!”

Import turtle as t rename it t when importing the turtle Library
 >>> t.circle(80)
 >>> t.forward(100)
 I'm a handsome man!!! ")

 

Let’s move on to the theme and draw a square:

 import turtle as t
 t.fd(100)
 t.left(90)
 t.fd(100)
 t.left(90)
 t.fd(100)
 t.lt(90)
 t.fd(100)

 

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

  -

Error demonstration:

 import turtle as t
 t.fd(100)
 t.left(90)
 t.fd(100)
 t.left(90)
 t.fd(100)
 t.lt(90)
 t.fd(100)
 #t.lt(90)
 
 t.penup()
 t.goto(-10,-10)
 t.pendown()
 
 t.fd(120)
 t.left(90)
 t.fd(120)
 t.left(90)
 t.fd(120)
 t.lt(90)
 t.fd(120)
 #t.lt(90)

 

  

 

 

The picture is beautiful, but it does not satisfy our requirement of the same center of gravity. The main reason for the problem is that the arrow direction does not return to the original direction after drawing the square.

Draw a bunch of squares:

Need to introduce our while loop, format:

  ”””

While condition:

The content of the cycle

  ”””

Among them, if the condition is established, we should pay attention to the semicolon on the content of the execution cycle. 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.

 import turtle as t
 
 t.speed(100)
 a = 0
 while a < 10:
     a = a +1
     t.fd(60 + a*20)
     t.left(90)
     t.fd(60 + a*20)
     t.left(90)
     t.fd(60 + a*20)
     t.lt(90)
     t.fd(60 + a*20)
     t.lt(90)
 
     t.penup()
     t.goto(-a*10,-a*10)
     t.pendown()

 

  

  

import turtle as t
 
 t.speed(100)
 a = 0
 while  10:
     a = a +1
     If a > 10:  Jump out of the loop in a > 10
         break
     t.fd(60 + a*20)
     t.left(90)
     t.fd(60 + a*20)
     t.left(90)
     t.fd(60 + a*20)
     t.lt(90)
     t.fd(60 + a*20)
     t.lt(90)
 
     t.penup()
     t.goto(-a*10,-a*10)
     t.pendown()

It was found that:The while loop itself has a judgment function

Picture axis:

Requirements, the negative end to – 300, the positive end to 300. Mark each 20 pixels

import turtle as t
 
 t.speed(100)
 t.write("0")
 t.goto(-300,0)
 t.goto(300,0)
 
 t.lt(90)
 T. circle (6, steps = 3) # Draw an arrow
 
 a = -300
 while a <= 300:
     t.goto(a,0)
     t.goto(a,-3)
     t.goto(a,3)
     t.goto(a,0)
     a = a + 20

  

 

 

On the basis of the number axis, draw a plane rectangular coordinate system:

import turtle as t
 
 t.speed(100)
 t.write("0")
 t.goto(-300,0)
 t.goto(300,0)
 
 t.lt(90)
 T. circle (6, steps = 3) # Draw an arrow
 
 # Draw the x-axis
 a = -300
 while a <= 300:
     t.goto(a,0)
     T. goto (a, -3) Draw markers
     t.goto(a,3)
     t.goto(a,0)
     a = a + 20
 
 t.penup()
 t.goto(0,-300)
 t.pendown()
 t.goto(0,300)
 t.lt(90)
 T. circle (6, steps = 3) # Draw an arrow
 
 # Draw Y axis
 a = -300
 while a <= 300:
     t.goto(0,a)
     t.goto(-3,a) 
     t.goto(3,a)
     t.goto(0,a)
     a = a + 20
 
 T. hideturtle ()# Hidden Arrow

  

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 essential for learning programming and mastering it. Any quick success and instant benefit can only achieve 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.

 

Recommended Today

Detailed explanation of spring cloud ribbon load balancing Shenkeng that cannot be connected through the server name

First, the problem. Take the Eureka cluster and the client calls the service through the ribbon. The ribbon side reports the following exceptions java.net.UnknownHostException: SERVICE-HI java.lang.IllegalStateException: No instances available for SERVICE-HI java.lang.IllegalStateException: Request URI does not contain a valid hostname: http://SERVICE-HI com.netfix.discovery.shared.taransport.TransportException: Cannot execute request on any known server The spring cloud version is messy, […]