Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

In their daily work and life, we often encounter one thing to repeat many times this happens. In programming, we will encounter this situation, cycle this mechanism is designed to deal with things that need to do is repeat this method. By reading a few minutes, you will master the mechanisms and programming techniques.

 

One thing to do is to repeat the cycle

First we look at a programming example, we now want to use the library in Python turtle to draw several rounds (if you do not know the turtle library can focus on the article before I see Oh, there is a detailed description of the entry)

First we look at the code

Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

The code we have repeatedly run the same code 4 times

Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

After the code runs to complete a total of four round draw

In the above code, which t.circle (100) and t.left (90) two lines of code, we have run a total of four times, which is actually repeatedly do.

Summary: To use the loop the first thing is to be clear in the end what is needed to do the cycle.

Clearly the first thing, let's continue. Now our goal is to draw four circles, which means we need four cycles, this is very easy to understand, and the second thing is we have to clear the number of cycles, here is 4.

Cycle four times, in the Python programming language, we have a variety of expressions. Before formally write cycle, we first look at the basic syntax of simply circulating in Python

for 循环的条件判断,当条件判断失败时就跳出循环,不再执行了:
循环体,也就是要循环做什么事情

The basic process execution cycle is the first step to determine the conditions for back, if the condition is true, then the loop body is executed if the condition is not satisfied, then out of the loop

Here we look at four specific cycle for how to deal with.

The first approach:

Behind the # symbol represents the following is a comment that is an explanation, not part of the operation code

i = 1 #声明一个变量,用来记录次数,初始值设置为1
for i < 5: #判断i的值是否小于5
t.circle(100)
t.left(90)
# 上面这两行是要循环做的事情
i = i + 1 # 每循环一次把i的值加1,作为次数的记录

The second approach:

Here we use a function, that is, the system provides us with a tool, it is the range

First we look at the effect of input range (4) In your editor, look at the operating results

Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

This is my operating results

This result means, contains a total of four values, their values ​​are 0, 1, 4 is not included here, we can use the array type is further proof, if you are new to programming, I do not know what is arrays, do not tangle, just remember the range (4) represents a set of numbers, they are 0,1,2,3 on it. If range (5), then it represents a set of numbers, they are not included 0,1,2,3,4 5

Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

The range (4) of the operation result into an array type

The second approach looks more simple and high-end a little bit, let's use the second approach to reprocess at the above code

Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

Refactored code

Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

Results of operation

The results of running the same as before us. Note that there is a wording for i in range (4), i is the meaning of this code results in the range (4) of years, in order to assign value (4) in the results to the i range, until range (4) of each result value will be taken to. That is, i'll values ​​are 0,1,2,3, which is the programmer mouth of said iteration means.

We have learned to cycle, let's draw a more beautiful rainbow circle it

Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

100 cycles

This piece of code, we set the number of cycles of 100 times, and modify the background becomes black, red, yellow and blue colors using Paint

Westward Programming: interesting cycle (Python language visualization turtle drawing demo)

The resulting code runs

Another point of view of painted mean.

This article describes the Python programming language to use for the cycle, there is another cycle, called the while loop, plus concern Oh, come follow explain. Collection and immediately to write your code it. A knowledge every day, bring you a taste of programming charm.

Guess you like

Origin www.cnblogs.com/lingfengblogs/p/11093305.html