• PL
  • EN

Co to jest pętla?

Gdy chcesz narysować wiele różnych kształtów, korzystając wyłącznie ze zdobytej wiedzy, czeka Cię dużo pracy. Pętla bardzo wiele ułatwia - kod jest znacznie krótszy.

import turtle
max = turtle.Turtle()

for i in range(4):
    max.fd(100)
    max.lt(90)


Ten kod pozwala czerokrotnie powtórzyć polecenia ruchu oraz obrotu, w wyniku czego powstanie kwadrat. Zobacz poniżej prawidłową konstrukcję pętli, by lepiej zrozumieć jej działanie.

Jak działa pętla?

W wyrażeniu: for i in range (x): należy zwrócić szczególną uwagę na:
i - to iterator, czyli zmienna pełniąca funkcję licznika.
x - to wartość, która jest przypisana do iteratora. Tyle razy pętla zostanie wykonana.
: - znak rozpoczęcia pętli. Bez dwukropka, całe wyrażenie nie zadziała.

Kolejnym bardzo ważnym elementem będącym cechą charakterystyczną dla języka PYTHON są tabulatory. Jeśli jakieś polecenia znajdują się w odległości jednego TABa od lewej krawędzi dokumentu oznacza to, że są elementem pętli lub funkcji.

ZAPAMIĘTAJ!

W języku PYTHON wcięcie w postaci klawisza TAB oznacza, że polecenie będzie powtarzane lub jest elementem funkcji.
Dwukrotne użycie tego klawisza może oznaczać, że kod znajduje się w pętli zagnieżdżonej. Może też zdarzyć się, że stworzono pętlę wewnątrz funkcji. Każdy kolejny TAB oznacza kolejny poziom zagnieżdżenia.

Nieuzasadnione użycie wcięcia (np. w celach wizualnych) spowoduje błędy w kodzie i w efekcie brak możliwości jego wykonania.

Praktyczne wykorzystanie pętli.

Na początek przykład bez pętli:

import turtle
max = turtle.Turtle()
bok = 100

max.fd(bok)
max.lt(90)
max.fd(bok/2)
max.lt(90)
max.fd(bok)
max.lt(90)
max.fd(bok/2)
max.lt(90)

Warto zwrócić uwagę o ile łatwiejszy i krótszy bedzie kod z wykorzystaniem pętli:

import turtle
max = turtle.Turtle()
for i in range(2):
    max.fd(100)
    max.lt(90)
    max.fd(50)
    max.lt(90)

Pętla powtarzana jest dwa razy, ponieważ prostokąt ma dwa boki krótkie i dwa dłuższe.

Kod na kwadrat będzie jeszcze krótszy i łatwiejszy:

import turtle
max = turtle.Turtle()
for i in range(4):
    max.fd(100)
    max.lt(90)

Trójkąt równoboczny w pętli.

Aby narysować trójkąt równoboczny przy pomocy pętli, należy zastanowić się jak to zrobić, aby w jak najmniejszej liczbie ruchów narysować tą figurę.

import turtle
max = turtle.Turtle()
for i in range(3):
    max.fd(100)
    max.lt(120)

What is a loop?

When you want to draw many different shapes using only the knowledge you have acquired, you have a lot of work ahead of you. The loop makes it much easier - the code is much shorter.

import turtle
max = turtle.Turtle()

for i in range(4):
    max.fd(100)
    max.lt(90)


This code allows you to repeat the move and rotate commands four times, resulting in a square is created. See below for proper loop construction to better understand how it works.

How the loop works?

In the expression: for i in range (x): special attention should be paid to:
i - is an iterator, that is, a variable that has the role of a counter.
x - is the value that is assigned to the iterator. This is the number of times the loop will be executed.
: - loop start character. Without a colon, the entire expression will not work.

Tab key is another very important feature of the PYTHON language. If some commands are located one TAB from the left edge of the document, it means they are part of a loop or function.

REMEMBER!

In the PYTHON language, an indentation in the form of TAB key. This means that the command will be repeated or is part of the function.
Using this key twice may mean that the code is in a nested loop. It may also happen that you have created a loop inside a function. Each successive TAB key means another level of nesting.

Unwarranted use of indentation (e.g. for visual purposes) will result in code errors and consequently inability to execution.

Practical use of loops.

At the beginning the example without loop:

import turtle
max = turtle.Turtle()
side = 100

max.fd(side)
max.lt(90)
max.fd(side/2)
max.lt(90)
max.fd(side)
max.lt(90)
max.fd(side/2)
max.lt(90)

It is worth noting how much easier and shorter the code will be using loops.:

import turtle
max = turtle.Turtle()
for i in range(2):
    max.fd(100)
    max.lt(90)
    max.fd(50)
    max.lt(90)

The loop is repeated twice, because the rectangle has two short sides and two long sides.

The code for the square will be even shorter and easier:

import turtle
max = turtle.Turtle()
for i in range(4):
    max.fd(100)
    max.lt(90)

Equilateral triangle in a loop.

To draw an equilateral triangle using a loop, you need to think about how to draw this figure in as few moves as possible.

import turtle
max = turtle.Turtle()
for i in range(3):
    max.fd(100)
    max.lt(120)