Chương 4: "for" loop with turtle program

import turtle
wn = turtle.Screen()
alex = turtle.Turtle()

for i in ['a', 'b', 'c', 'd']:
    alex.forward(50)
    alex.left(90)
--------
Trường hợp này thì nó sẽ đi 4 vòng vì lặp 4 lần - tạo thành hình vuông (do có đoạn dài như nhau).


import turtle
wn = turtle.Screen()
alex = turtle.Turtle()

for color in ['yellow', 'blue', 'red', 'purple']:
    alex.color(color)
    alex.forward(50)
    alex.left(90)

---------------------
Trường hợp này thì nó sẽ đi 4 vòng và đổi màu ở mỗi vòng. Chú ý ở đây là:
câu lệnh alex.color và giá trị color trong vòng.


import turtle
wn = turtle.Screen()
alex = turtle.Turtle()

for i in range(4):
    alex.forward(50)
    alex.left(90)

tương tự. như ở trên nhưng ở đây chúng ta sử dụng hàm range().

----------------------------------

for name in ["Joe", "Amy", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]:
    print("Hi", name, "Please come to my party on Saturday!")



Take a look at the output produced when you press the run button. There is one line printed for each friend. Here’s how it works:
  • name in this for statement is called the loop variable.
  • The list of names in the square brackets is called a Python list. Lists are very useful. We will have much more to say about them later.
  • Line 2 is the loop body. The loop body is always indented. The indentation determines exactly what statements are “in the loop”. The loop body is performed one time for each name in the list.
  • On each iteration or pass of the loop, a check is done to see if there are still more items to be processed. If there are none left (this is called the terminating condition of the loop), the loop has finished. Program execution continues at the next statement after the loop body.
  • If there are items still to be processed, the loop variable is updated to refer to the next item in the list. This means, in this case, that the loop body is executed here 7 times, and each time name will refer to a different friend.
  • At the end of each execution of the body of the loop, Python returns to the for statement, to see if there are more items to be handled.




Nhận xét

Bài đăng phổ biến từ blog này

2.7. Operators and Operands - toán tử và toán hạng

1.11. Formal and Natural Languages

3.10 Stack diagrams - sơ đồ xếp chồng