You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/_sources/conditionals.txt
+20-23Lines changed: 20 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Conditional statements
4
4
Introduction
5
5
============
6
6
7
-
So far we have accomplished predefined tasks, but in all honesty we were accomplishing no better achievements than the 18th century automate, or the music boxes following one set of instruction to the end. What makes programming so much more powerful is conditional statements. This is the ability to *test* a variable (or a name) against a value and act in one way if the condition is met by the variable or an other way if not. They are also commonly called by programmers *if statements*.
7
+
So far we have accomplished predefined tasks, but in all honesty we were accomplishing no better achievements than the 18th century automata, or the music boxes following one set of instruction to the end. What makes programming so much more powerful is conditional statements. This is the ability to *test* a variable (or a name) against a value and act in one way if the condition is met by the variable or an other way if not. They are also commonly called by programmers *if statements*.
8
8
9
9
To know if a condition is *True* of *False*, we need a new type of data:
10
10
the booleans. They allow logical operations.
@@ -28,10 +28,10 @@ Here are some simple examples::
28
28
29
29
direction = -30
30
30
if direction > 0 :
31
-
forward(direction)
31
+
turtle.forward(direction)
32
32
else:
33
-
left(180)
34
-
forward (-direction)
33
+
turtle.left(180)
34
+
turtle.forward (-direction)
35
35
36
36
37
37
Condition an action
@@ -47,22 +47,22 @@ We want the turtle to go forward for a specified distance (taken as a function p
47
47
.. note::
48
48
* We start from the center of the screen, which has coordinate (0;0)
49
49
* We won't turn, until we reach the boundary our turtle wants to get away as quickly as possible
50
-
* We can get the x coordinate of our turtle with the function *xcor*
51
-
* We can get the width of the screen with the function *window_width*
50
+
* We can get the x coordinate of our turtle with the function ``turtle.xcor()``
51
+
* We can get the width of the screen with the function ``turtle.window_width()``
@@ -71,27 +71,24 @@ Now when the turtle reaches the end of the screen, it turns around and
71
71
continue its movement in the other direction, the remaining of the distance
72
72
73
73
.. note::
74
-
* heading() gives you the current angle the turtle's direction makes with the original east facing turtle. (a turtle going straight up has a heading of 90 degrees)
74
+
* turtle.heading() gives you the current angle the turtle's direction makes with the original east facing turtle. (a turtle going straight up has a heading of 90 degrees)
Copy file name to clipboardExpand all lines: en/_sources/functions.txt
+30-30Lines changed: 30 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,8 @@ There is still a lot of duplicated code --- the actual drawing of the rectangle
9
9
lacking abstractions. (Programmers call it a *code smell.*)
10
10
11
11
Functions are one way to express abstractions in Python. Let's take
12
-
``reset()`` for example. It is actually an abstraction for a number of steps,
13
-
namely:
12
+
``turtle.reset()`` for example. It is actually an abstraction for a number of
13
+
steps, namely:
14
14
15
15
* Erase the drawing board
16
16
* Set the width and color back to default
@@ -19,15 +19,15 @@ namely:
19
19
A function can be defined with the ``def`` keyword in Python::
20
20
21
21
def line_without_moving():
22
-
forward(50)
23
-
backward(50)
22
+
turtle.forward(50)
23
+
turtle.backward(50)
24
24
25
25
You can access names in functions as well::
26
26
27
27
size = 50
28
28
def line_without_moving():
29
-
forward(size)
30
-
backward(size)
29
+
turtle.forward(size)
30
+
turtle.backward(size)
31
31
32
32
.. note:: Python uses *whitespaces to identify blocks of code* belonging together. While other languages use special characters (like curly brackets) in python a block is introduced with a colon at the end of the line and commands within a deeper identation level - ususally 4 spaces. The block ends with the first line with a lesser identation level.
Copy file name to clipboardExpand all lines: en/_sources/functions_parameters.txt
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -10,14 +10,14 @@ That is why we need to be able to give parameters, or also called *arguments* so
10
10
Remember how we defined the function ``line_without_moving`` in the previous section::
11
11
12
12
def line_without_moving():
13
-
forward(50)
14
-
backward(50)
13
+
turtle.forward(50)
14
+
turtle.backward(50)
15
15
16
16
We can improve it by giving it a parameter::
17
17
18
18
def line_without_moving(length):
19
-
forward(length)
20
-
backward(length)
19
+
turtle.forward(length)
20
+
turtle.backward(length)
21
21
22
22
The parameter acts as a *name* only known inside the function's definition. We use the newly defined function by calling it with the value we want the parameter to have like this::
23
23
@@ -29,10 +29,10 @@ We have been using functions with parameters since the beginning of the tutorial
29
29
30
30
And we can put as many arguments (or parameters) as we want, separating them with commas and giving them different names::
31
31
32
-
def tilted_line_without_moving(length,angle):
33
-
left(angle)
34
-
forward(length)
35
-
backward(length)
32
+
def tilted_line_without_moving(length,angle):
33
+
turtle.left(angle)
34
+
turtle.forward(length)
35
+
turtle.backward(length)
36
36
37
37
38
38
A parameterized function for a variable size hexagon
0 commit comments