Y FIGURE 4.1 The Word Jumble game. This jumble looks “difficult.
” This game re-creates the typical word
jumble you might find in the Sunday paper (you know, that thing people used to read before the
Internet). The computer picks a random word from a group and then creates a jumbled version of it,
where the letters are in random order. The player has to guess the original word to win the game. USING
FOR LOOPS In the last chapter, you saw one kind of loop, the while loop, which repeats part of your
code based on a condition. As long as the condition is true, some code repeats. The for loop also repeats
code, but not based on a condition. Instead, the for loop repeats part of a program based on a
sequence, an ordered list of things. If you’ve ever written a list of, say, your top 10 favorite movies, then
you’ve created a sequence. A for loop repeats its loop body for each element of the sequence, in order.
When it reaches the end of the sequence, the loop ends. As an example, consider your movie list
sequence again. A for loop could go through this sequence of movie titles, one at a time, and print each
one. But the best way to understand a for loop is to see one in action. Introducing the Loopy String
Program This program takes a word from the user and prints its letters, in order, on separate lines. Look
at a sample run in Figure 4.2. 88 Python Programming for the Absolute Beginner, Third Edition FIGURE
4.2 A for loop goes through a word the user enters, one character at a time. This simple program
provides a good example of a for loop. You can find the code for the program on the companion website
(www.courseptr.com/downloads) in the Chapter 4 folder; the file name is loopy_string.py. # Loopy
String # Demonstrates the for loop with a string word = input("Enter a word: ") print("\nHere's each
letter in your word:") for letter in word: print(letter) input("\n\nPress the enter key to exit.")
Understanding for Loops The new idea in this program is the for loop, which is just the following two
short lines: for letter in word: print(letter) Even before you know anything about for loops, the code is
pretty clear, but I’ll explain exactly how it works. All sequences are made up of elements. A string is a
sequence in which each element is one character. In the case of the string "Loop", the first element is
the character "L", the second is "o", and so on. Chapter 4 • for Loops, Strings, and Tuples: The Word
Jumble Game 89 A for loop marches through (or iterates over) a sequence one element at a time. In my
program, the loop iterates over the string "Loop", one character at a time. A for loop uses a variable that
gets each successive element of the sequence. In my loop, letter is the variable that gets each successive
character of "Loop". Inside the loop body, the loop can then do something with each successive
element. In my loop body, I simply print letter. Now, the variable you use to get each element of the
sequence is like any other variable—and if it doesn’t exist before the loop, it’s created. So, when my
loop begins, letter is created and gets the first character in word, which is "L". Then, in the loop body,
the print statement displays L. Next, with the loop body finished, control jumps back to the top of the
loop and letter gets the next character in word, which is "o". The computer displays o, and the loop
continues until each character in the string "Loop" is displayed. In the Real World Most modern
languages offer a form of the for loop. However, these loops tend to be more restrictive. The loops
generally only allow a counter variable, which must be assigned a number. Then, the counter changes by
the same amount, each time the loop executes. The ability to loop directly through a sequence makes
the Python for loop more flexible than this other, more traditional type of loop. Creating a for Loop To
create a for loop, you can follow the example in the program. Start with for, followed by a variable for
each element, followed by in, followed by the sequence you want to loop through, followed by a colon,
and finally, the loop body. That’s all there is to it. COUNTING WITH A FOR LOOP When you write a
program, you may find that you need to count. In combination with the for loop, you can use Python’s
range() function to count in all kinds of ways.