.. _pythonPrimerPart2: Python Primer - Part 2 ====================== Talking to your Program ^^^^^^^^^^^^^^^^^^^^^^^ As usual, we will start with a small example. Run the following code bellow and type in different values. .. literalinclude:: ../examples/turtle/turtle_loop_io.py :linenos: :language: python **From the above code, can you deduce which functions allow you to interract with your program?** 1. **print()** Allows you to display a message on the Terminal. The message can be composed by text or a value, or a combination. 2. **input()** Not only displays a message to the Terminal, but also reads what the user types in! In our simple example, the number of steps is read and stored in the variable *steps*. Challenge 1 ^^^^^^^^^^^ **Play with the input and output function and develop a small program, which requires interaction with the user!** Here is a small example: .. image:: _static/img/age.gif :align: center As inspiration, here is the code as well: .. literalinclude:: ../examples/turtle/age.py :linenos: :language: python .. tip:: No programmer knows all functions and how to use them by heart. Therefore, keep in mind that **Google is your friend!** .. note:: In both examples above, an **endless loop** was used: **while(1)**. **'1'** in programming means *true*, as opposed to **'0'** which means *false*. Setting the loop condition to 1, means that it is always true and will go on... **forever**! Not very clear yet? Let us learn more about conditions in the next section, and everything will make more sense. Making Decisions ^^^^^^^^^^^^^^^^ Yes or No? ---------- Part of our human intelligence is based on our capacity to **take decisions**, given certain **conditions** that surround us, and **act accordingly**. Does the example bellow look familiar? .. image:: _static/img/condition_food.png :align: center The idea is to give part of this intelligence to the computer program, so that it can make decisions on its own! .. note:: In programming languages, this is done with **conditional statements**: **IF - THEN - ELSE** ==================== ============================================================================== Block Description ==================== ============================================================================== IF Checks if the condition is **true**. THEN If the condition is **true**, this action is executed. ELSE Otherwise, if the condition is **not true**, this other this other action is executed. ==================== ============================================================================== .. image:: _static/img/condition_food_if.png :align: center Let us illustrate this with our Turtle example from above, we will make it more **intelligent**. Do you remember how the turtle would always go left? Annoying, huh? **Why not ask the user which direction he wants to go, left or right?** .. image:: _static/img/turtle_left_right.gif :align: center .. note:: Our turtle is now intelligent enough to go left OR right, depending on the user's input! Have a look at how this is done in Python... .. literalinclude:: ../examples/turtle/turtle_loop_io_conditions.py :linenos: :language: python In the above example... - What is condition? - What happens if the condition is fulfilled? - What happens if the condition is not fulfilled? Else, if... ----------- Alright, you type in "r", turlte goes right. You type in "l", turlte goes left. That is what we wanted. **Have you tried typing in any other letter?** **What happens then?** **Looking back at the code, can you figure out why?**