程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How Python uses for loop and turtle drawing to realize beautiful spiral

編輯:Python

python How do you use it? for Loop and turtle drawing to achieve a beautiful spiral

This article “python How do you use it? for Loop and turtle drawing to achieve a beautiful spiral ” Most people don't quite understand the knowledge points of the article , So I made up the following summary for you , Detailed content , The steps are clear , It has certain reference value , I hope you can gain something after reading this article , Let's take a look at this article “python How do you use it? for Loop and turtle drawing to achieve a beautiful spiral ” Article bar .

use for Loop and turtle drawing to achieve a beautiful spiral

A. Course content

In this lesson, we will draw complex spiral lines to learn more for Circulation and range() Function usage . Learn more about the process of the loop .

B. Knowledge point

(1)for Usage of loop

(2)range() Function usage

(3) Draw a spiral

C. Basic instructions used

(1)import

(2)turtle.Pen()

(3)forward()

(4)right()

(5)for loop

(6)range()

We have already talked about how to draw a regular pentagon , This section combines for Loop to draw a Pentagon helix . The characteristic of a helix is that it has many sides , Each side has a different length , Is gradually increasing . For example 1 Side length is 1, The first 2 Side length is 2, The first 3 Side length is 3, In turn increase . If you use for loop , Then the distance to go forward in each cycle increases 1, As shown in the figure :

range(100) Will let for The code after the colon in the loop loops 100 Time .

Output Pentagon helix

How does the above program execute ?

(1) First, import the turtle drawing module , And change the t Set to turtle strokes .

(2)python The first time a loop is executed ,for Loop first accesses range The first element in the list 0(range(100) Will generate from 0 To 99 A continuous number of ), And will 0 Stored in variables x in , Variable x The value of a 0.

(3) perform t.forward(x) Statement to move the turtle brush forward x Step . because x=0, So the brush moves forward 0 Step .

(4) perform t.right(72) sentence , Turn the turtle brush to the right 72°.

(5) Perform the second cycle ,for Loop access range The second element in the list 1, And will 1 Stored in variables x in , Variable x The value of a 1. Then the brush moves forward 1 Step , Turn the brush to the right 72°.

(6) Perform the third cycle ,for Loop access range The third element in the list 2, And will 2 Stored in variables x in , Variable x The value of a 2, Then the brush moves forward 2 Step , Turn the brush to the right 72°.

(7) This continues to be done again and again , When for Loop traversal range The last element in 99 when , Variable x The value is 99, The brush moves forward 99 Step , And turn it to the right 72°. This will draw a beautiful spiral line .

Draw rotation angle modification

In the program above , If we change the rotation angle of each drawing, what figure will be drawn ?

(1) Rotate the angle angle It is amended as follows 76° when :

(2) Rotate the angle angle It is amended as follows 91° when :

That's about “python How do you use it? for Loop and turtle drawing to achieve a beautiful spiral ” The content of this article , I believe we all have a certain understanding , I hope the content shared by Xiaobian will be helpful to you , If you want to know more about it , Please pay attention to the Yisu cloud industry information channel .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved