Meeting 2020-02-18

Looping in Python

Last week we learned how to change blocks in the Minecraft world using the Python command, but writing a line of code to set every single block would get tedious fast. Fortunately, computer programs are really good at doing repetitive tasks, and Python includes a looping keyword called for that can be used to repeatedly execute a block of program code over a range of values. The values can be numbers, letters in a string, or items in a data structure, but to start with we will focus on looping over a range of numbers generated by the range() function.

For loops

A simple for structure looks like this:

for x in range(6):

print(x)

Right after the for keyword is a variable that will take on the different values in the sequence so they can be used in the loop code. The function range(6) will generate the numbers 0, 1, 2, 3, 4, 5 and Python will execute the code after the colon (:) once for each of those six values. Note that counting starts at zero and goes up to but not including the number that is the argument to range() - this is typical Python behavior for indices.

The lines of code after the colon that are to be looped over need to be indented - add a couple of spaces or press the TAB key at the beginning of the line. Indents are very important in Python, all the program lines you want to have executed in the loop need to be indented by the same amount, and Python ends the loop after the indentation goes back to its previous level. This is different than most programming languages that have a pair of characters like {curly braces} that define a block of code to loop over.

Open your IDLE Python shell and enter the above lines into the REPL prompt. You'll see that when the shell sees a line that ends with a colon (:), it knows to not execute it right away and get more lines of input for the loop. You can enter as many lines as you want and press the ENTER key on a blank line to finish the loop.

>>> for x in range(6):

print(x)

0

1

2

3

4

5

>>>

The Range function

If you just want to loop a certain number of times but don't need to do anything with the loop variable you can just use range(numLoops)with a single argument, and the loop variable will take values 0 -> numLoops-1 . But if you want to specify the starting and ending value of the loop variable you can:

>>> for x in range(5,10):

print(x)

5

6

7

8

9

>>>

Note that the loop starts with the first argument to range() and goes to one less than the second argument, so the total number of steps in range(startVal,endVal) is (endVal-startVal). Get used to this, it is how almost everything in Python does counting.

If you don't want to count by 1's you can specify a third argument to range():

>>> for x in range(5,55,10):

print(x)

5

15

25

35

45

>>>

Note that Python doesn't get to the final value even if it is a multiple of the step size - it only goes to (endVal-1) .

Project: Zombie Tower Using a For Loop

As a first project using a for loop, redo the program we wrote last week to build a tower of dirt blocks and teleport the player to the top of it. You can download a copy of last week's program zombietower.py at the bottom of this web page if you don't have a copy on your computer. Add a constant TOWERHEIGHT = 4 at the beginning of the program and replace the multiple calls to mc.setblock() with different Y coordinates with a single call inside a for loop. In the range() function, start at playerPos.y and loop until playerPos.y+TOWERHEIGHT, and then teleport the player to playerPos.y+TOWERHEIGHT+2.

Nested Loops

You can put a for loop inside another for loop so that the inner for goes all the way through its loop for each value of the outer for. This is called nested loops. The code for the inside loop should be indented two levels - twice as many spaces or two presses of the tab key. Nested loops are useful when you have data with two different degrees of freedom or dimensions.

>>> for x in range(1,5):

for y in range(1,5):

print( x * y )

1

2

3

4

2

4

6

8

3

6

9

12

4

8

12

16

>>>

Project: Building a Sky Platform

We can use nested arrays to build a two-dimensional sky platform that will help us hide from skeletons and spiders as well as zombies. Replace the loop in the Zombie Tower program with nested loops over the x and z position of the blocks. A single mc.setBlock() call in the inside of the nested loops will build a square platform of blocks.