.. _thinkLikeAProgammer: Think like a Programmer ======================= Lea and Max ^^^^^^^^^^^ Let us start with an example. Have a look at the animation bellow: .. image:: _static/img/turtle_unplugged.gif **What do you see?** **What is happening?** Lea is calling out **instructions** and Max is following them. We can say that Max is **executing** Lea's instructions. Let us write down the complete **sequence** of **instructions** that Lea calls out to Max:: Forward - Turn left - Forward - Turn right - Forward - Turn left - Forward - Turn left - Forward - Forward - Turn left - Forward 2 steps - Turn left. We will call the above sequence of instructions **code**. Notice that Lea used 4 different instructions in her code, let us right them down here: +-------------------+-----------------------------+ |**Forward** | Take 1 step forward | +-------------------+-----------------------------+ |**Forward 2 steps**| Take 2 steps forward | +-------------------+-----------------------------+ |**Turn left** | Turn left by 90 degrees | +-------------------+-----------------------------+ |**Turn right** | Turn right by 90 degrees | +-------------------+-----------------------------+ .. _challenge1: Challenge 1 ^^^^^^^^^^^ Like Lea and Max, try to crack the code given bellow. Follow the instructions and, on a sheet of paper, move the turtle (black arrow) according to the code. What shape do you obtain? :: Forward - Turn right - Forward - Turn left - Forward - Turn left - Forward - Turn right - Forward - Turn right - Forward - Forward - Turn right - Forward - Turn right - Forward - Turn left - Forward - Turn left - Forward - Turn right - Forward - Turn right - Forward - Forward - Turn right. .. image:: _static/img/grid_empty.svg :width: 400px :align: center :height: 400px .. note:: After executing the code, the turtle is a the same position and looks in the same direction as before you executed the code. From now on, we will talk about **start position** and **start direction**. .. _challenge2: Challenge 2 ^^^^^^^^^^^ Can you write down the code to draw the shape given bellow? .. image:: _static/img/grid_shape_S.svg :width: 400px :align: center :height: 400px .. important:: Remember that your turtle should be in the **start position** and look in the **start direction** after the execution of your code! .. note:: There is **more than one** correct code to draw this shape, depending for instance in which direction you decide to move your turtle or which instructions you use. Try to find the shortest possible code! The Python Turtle ^^^^^^^^^^^^^^^^^^ Now that you are a master in cracking and writing codes to move any turtle around, it is time to meet Python's turtle! To do so, open :any:`pythonShell` and import the **turtle library** by typing the following line: .. code-block:: python from turtle import * We are now ready to play with our Python turtle. Type in: .. code-block:: python forward(100) The following window will open: .. image:: _static/img/turtle_fd100.png :align: center Congratulations, your turtle just executed its first instruction! Let us have a closer look at the instructions you just typed in: +-------------------------+---------------------------------------+ |**forward(distance)** | Moves the turtle forward by *distance*| +-------------------------+---------------------------------------+ .. note:: *forward()* is called a **function** and *distance* is the function **parameter**. **Can you guess what the other instructions Lea used earlier to guide Max will look like?** Here is a table with Lea's instructions translated to Python: ================== ========================== ========================= Lea and Max Python Turtle Description ================== ========================== ========================= Forward forward(100) 1 step forward Forward 2 steps forward(200) Take 2 steps forward Turn left left(90) Turn left by 90 degrees Turn right right(90) Turn right by 90 degrees ================== ========================== ========================= .. note:: If you want to make bigger or smaller steps, you can replace the *distance* parameter of the *forward(distance)* function by any value! **Now that you know all the instructions, can you re-draw Max and Lea's shape with your turtle?** Here is some help... Make sure to understand this example to be able to solve :ref:`challenge3`! .. image:: _static/img/turtle_example.gif :align: center .. hint:: The following function will clear the screen and place the turtle at its start position: **reset()** .. _challenge3: Challenge 3 ^^^^^^^^^^^ As in the example above, draw the shape from :ref:`challenge1` with the Python turtle! .. image:: _static/img/grid_shape_H.svg :width: 200px :align: center :height: 200px Can you do it for the shape of :ref:`challenge2` as well? .. image:: _static/img/grid_shape_S.svg :width: 200px :align: center :height: 200px Write a Python Script ^^^^^^^^^^^^^^^^^^^^^ You may have noticed that each time you made a mistake, you would have to enter all instructions from scratch. In order to avoid this, we will now create a **Python script** with our instructions. A script, or program, is simply a sequence of instructions that is saved somewhere on your Pi. When you **run** a script, the instructions are executed one by one in the order they are written. **Can you write and run the script for Max and Lea's example?** Here is a possible solution: .. literalinclude:: ../examples/turtle/turtle_example.py :linenos: :language: python Challenge 4 ^^^^^^^^^^^ Try to imagine any basic shape (square, circle, triangle, hexagons, etc...) and then draw it using the turtle! More Turtle Functions ^^^^^^^^^^^^^^^^^^^^^ Now that you are familiar with the functions introduced so far, **forward(distance), left(angle), right(angle)**, try out the functions listed in the table bellow. **Can you describe shortly what they do?** ==================== ====================================================== Python Turtle Description ==================== ====================================================== backward(distance) Makes the turtle go back a distance setx(xpos) Moves the turtle along the same y to a specified x sety(ypos) Moves the turtle along the same x to a specified y goto(x,y) Moves the turtle to a specific x and y ==================== ====================================================== .. tip:: You will find all the answers and more turtle functions here: http://www.eg.bucknell.edu/~hyde/Python3/TurtleDirections.html