Posts

Conditionals and loops

In Python we can use the if statement as follows:

if a>b:
    print(a)

In contrast to other programming languages Python does not use {} to declare the opening or the closing of an if statement. Python uses indentation (tab key or four spaces). Python’s indentation is a way to tell Python interpreter that a group of statements belong to a block of code. In the previous example the variable a will be printed if a is greater than b.

In the next piece of code the lines 2 and 3 will be executed if a is greater than b and the lines 5 and 6 will be executed if a is not greater than b. Indentation in Python matters!

if (a>b):
    print(a)
    print ("Hello")
else:
    print("b")
    print("Goodbye")

We can always examine more than 2 logical conditions with the use of elif (stands for else if ).

if (a>b):
    print(a)
elif (b>a):
    print(b)
else:
    print("Equals")

Python will create automatically an indent if we end the previous block of code with a colon “:”

A colon is used in conditionals (if statements) as well as in loops (while, for, etc) to declare which lines of code belong to a specific logical expression.

We can also combine logical expressions using the and or the or operator to create more interesting statements. In the following example line 2 will be executed if both of the expressions (age>4 and age <=10) are true.

if age > 4 and age <= 10:
    print('Best age to learn programming')

Loops

An if statement is useful if we need to check a logical statement once. If we want to keep executing a set of code bocks until a specific logical expression comes true, we have to use a loop.

while

The most common loop statement is the while. In the following example, the while statement will be true as long as the variable a is less than 10.

a=1
while (a<10):
    print(a)
    a = a+1

For loop

for i in range(10):
    name = input('What is your name?')
    if name == 'John':
        print('Hello John')

 

Conditionals and loops

Detect and Inspect

These three instructions are useful to decide what or if there is a block near the turtle.

The detect command detects if there is an existing block ahead, up or below the turtle. If there is not, the turtle can move. Else, you may use the dig command to destroy the block.

The following program will make the turtle move 10 blocks. If there is an obstacle, the turtle will destroy it.

The rep command stands for repeat.

Repeat is a block which must be written as following:

rep <number> do

<instructions>

end

Inside a repeat loop you can nest other instructions as well.

In the next example the turtle will check 10 times if there is a block ahead; if it is true, it will break the block; in any case it will move 10 blocks forward.

The if statement is a conditional. It is checked once. If it is true it will execute whatever inside the command. An if statement must be written as following:

if <condition> then

<instructions>

end

The next program is an example of the inspect block. While the turtle inspects the block ahead and it is not a block of cobblestone, the turtle moves.

The turtle will stop moving when face a cobblestone block.

The while loop is the second iterative control statement.

A while loop must be written as following:

while <condition> do

<instructions>

end

In the while loop the instructions inside are executed as long as the condition is true. The moment the condition turns false there will be an exit from the loop.

In a repeat loop you must state in advance the number of repetitions. In a while loop this number is unknown as the condition may turn false at any point.

Build a bridge with the while loop

The following code will make the turtle build a bridge, provided that the turtle moves in the air.

In particular, the turtle detects if there is a block below.

If not, the turtle places a block from the first slot of its inventory.

It is required that there are some block items (stone or dirt etc) in the turtle’s inventory.

A fully functional program is the following.

The turtle will keep moving until detects a block.

If below the turtle is nothing, a block will be placed.

This way the turtle will build a bridge while walking in the air.

Of course, if there is nothing ahead the turtle will keep moving forward forever.

 

 

 

Download the lesson in pdf