Introducing the Word Jumble Game


The Word Jumble game, featured in Figure 4.1, utilizes many of the new ideas you'll learn in this chapter.

click to expand
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 get their news from 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. Take a look at a sample run in Figure 4.2.

click to expand
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. Here's the code:

 # Loopy String # Demonstrates the for loop with a string # Michael Dawson - 1/26/03 word = raw_input("Enter a word: ") print "\nHere's each letter in your word:" for letter in word:     print letter raw_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. Any string, like the one I entered, "Loop", is really a sequence. All sequences are made up of elements. For strings, each element is one character. In this case, the first element is the character "L", the second is "o", and so on. Since a for loop goes through a sequence one element at a time, this loop goes through the letters in "Loop" one at a time. To begin, letter gets the first character in word, which is "L". Next, the loop body, which is just the print statement, displays L. Then, 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.

start sidebar
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.

end sidebar

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.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net