The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 5 Programming with Python 97

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


print("Loop finished!")

Your four-line program is now complete. The first line sits outside the loop, and will only run
once; the second line sets up the loop; the third sits inside the loop and will run once for each
time the loop loops; and the fourth line sits outside the loop once again.


print("Loop starting!")
for i in range ( 10 ):
print("Loop number", i)
print("Loop finished!")

Click the Run icon, save the program as Indentation, and view the shell area for its
output (Figure 5-3):
Loop starting!
Loop number 0
Loop number 1
Loop number 2
Loop number 3
Loop number 4
Loop number 5
Loop number 6
Loop number 7
Loop number 8
Loop number 9
Loop finished!


5 Figure 5-3: Executing a loop

Free download pdf