# Chapter 3 : For Loops, Strings, and Arrays: Word Jumble¶

### example 3.1 page no :83¶

In [1]:
print  "Counting forward:\n";
for i in range(10):
print  i ,
print  "\n\nCounting backward:\n";
for i in range(1,10,-1):
print  i,

print  "\n\nCounting by fives:\n";
for i in range(0,51,5):
print  i,

print  "\n\nCounting with null statements:\n";
count = 0;
while (  count < 10 ):
print  count ,
count += 1

print  "\n\nCounting with nested for loops:\n";
ROWS = 5;
COLUMNS = 3;
for i in range(ROWS):
for j in range(COLUMNS):
print  i , "," , j ,

Counting forward:

0 1 2 3 4 5 6 7 8 9

Counting backward:

Counting by fives:

0 5 10 15 20 25 30 35 40 45 50

Counting with null statements:

0 1 2 3 4 5 6 7 8 9

Counting with nested for loops:

0 , 0 0 , 1 0 , 2 1 , 0 1 , 1 1 , 2 2 , 0 2 , 1 2 , 2 3 , 0 3 , 1 3 , 2 4 , 0 4 , 1 4 , 2


### example 3.2 page no : 90¶

In [2]:
word1 = "Game";
word2 ="Over"
word3 = '!'
phrase = word1 + " " + word2 + word3;
print  "The phrase is: " , phrase , "\n";
print  "The phrase has " , len(phrase) , " characters in it.\n";
print  "The character at position 0 is: " , phrase[0] , "\n\n";
print  "Changing the character at position 0.\n";
#phrase[0] = 'L';
print  "The phrase is now: " , phrase , "\n"
for i in range(len(phrase)):
print  "Character at position " , i , " is: " , phrase[i]

print  "\nThe sequence 'Over' begins at location ";
print  phrase.find("Over")
if ("eggplant" not in phrase):
print  "'eggplant' is not in the phrase.\n\n";

# Python does not support string erase function.
#phrase.erase(4, 5);
print  "The phrase is now: " , phrase
#phrase.erase(4);
print  "The phrase is now: " , phrase
#phrase.erase();
print  "The phrase is now: " , phrase
if (phrase==''):
print  "\nThe phrase is no more.\n";

The phrase is:  Game Over!

The phrase has  10  characters in it.

The character at position 0 is:  G

Changing the character at position 0.

The phrase is now:  Game Over!

Character at position  0  is:  G
Character at position  1  is:  a
Character at position  2  is:  m
Character at position  3  is:  e
Character at position  4  is:
Character at position  5  is:  O
Character at position  6  is:  v
Character at position  7  is:  e
Character at position  8  is:  r
Character at position  9  is:  !

The sequence 'Over' begins at location
5
'eggplant' is not in the phrase.

The phrase is now:  Game Over!
The phrase is now:  Game Over!
The phrase is now:  Game Over!


### example 3.3 page no : 97¶

In [3]:
MAX_ITEMS = 10;
inventory = [];

inventory.append("sword")
inventory.append("armor")
inventory.append("shield")
for i in range(len(inventory)):
print inventory[i]

inventory[0] = "battle axe";
for i in range(len(inventory)):
print inventory[i]

print "\nThe item name '" , inventory[0] , "' has ",
print len(inventory[0]) , " letters in it.\n";
print "\nYou find a healing potion.";
if (len(inventory) < MAX_ITEMS):
inventory.append("healing potion")
else:
print "You have too many items and can't carry another.";
for i in range(len(inventory)):
print inventory[i]

Your items:

sword
armor
shield

battle axe
armor
shield

The item name ' battle axe ' has  10  letters in it.

You find a healing potion.

battle axe
armor
shield
healing potion


### example 3.4 page no : 104¶

In [4]:
ROWS = 3;
COLUMNS = 3;
board = [['O', 'X', 'O'],[' ', 'X', 'X'],['X', 'O', 'O']]
print "Here's the tic-tac-toe board:\n";
for i in range(ROWS):
for j in range(COLUMNS):
print board[i][j],
print ''

print "\n'X' moves to the empty location.\n\n";
board[1][0] = 'X';
print "Now the tic-tac-toe board is:\n";
for i in range(ROWS):
for j in range(COLUMNS):
print board[i][j],
print ''
print "\n'X' wins!";

Here's the tic-tac-toe board:

O X O
X X
X O O

'X' moves to the empty location.

Now the tic-tac-toe board is:

O X O
X X X
X O O

'X' wins!


### example 3.5 page no : 107¶

In [1]:
fields = ['WORD', 'HINT', 'NUM_FIELDS']
NUM_WORDS = 5;
WORDS = [ ["wall", "Do you feel you're banging your head against something?"],
["labored", "Going slowly, is it?"],
["persistent", "Keep at it."],
["jumble", "It's what the game is all about."]]

class difficulty:
EASY = 0
MEDIUM = 1
HARD = 2
NUM_DIFF_LEVELS = 3

print  "There are " , difficulty.NUM_DIFF_LEVELS  ," difficulty levels."

import random
#srand(static_cast<unsigned int>(time(0)));
choice = (int(random.random() * 10) % NUM_WORDS);
theWord = WORDS[choice][0]; # word to guess
theHint = WORDS[choice][1]; # hint for word
jumble = theWord; # jumbled version of word
length = len(jumble)
for i in range(length):
index1 = (int(random.random() * 10) % length);
index2 = (int(random.random() * 10) % length);
temp = list(jumble)
temp[index1],temp[index2] = temp[index2],temp[index1]
jumble = ''.join(temp)

print "\t\t\tWelcome to Word Jumble!\n\n";
print "Unscramble the letters to make a word.\n";
print "Enter 'hint' for a hint.\n";
print "Enter 'quit' to quit the game.\n\n";
print "The jumble is: " , jumble;

guess = raw_input()

while ((guess != theWord) and (guess != "quit")):
if (guess == "hint"):
print theHint;
else:
print "Sorry, that's not it.";
guess = raw_input()

if (guess == theWord):
print  "\nThat's it! You guessed it!\n";

print  "\nThanks for playing.\n";

There are  3  difficulty levels.
Welcome to Word Jumble!

Unscramble the letters to make a word.

Enter 'hint' for a hint.

Enter 'quit' to quit the game.

The jumble is:  gasslse