Chapter 4 : The Standard Template Library: Hangman

example 4.1 page no : 118

In [1]:
inventory = []
inventory.append("sword");
inventory.append("armor");
inventory.append("shield");
print "You have " , len(inventory) , " items.";
print "\nYour items:";
for i in inventory:
    print i

print "\nYou trade your sword for a battle axe.";
inventory[0] = "battle axe";
print "\nYour items:\n";
for i in inventory:
    print i

print "\nThe item name '" , inventory[0] , "' has ",
print len(inventory[0]) , " letters in it.";
print "\nYour shield is destroyed in a fierce battle.";
inventory.pop();
print "\nYour items:";
for i in inventory:
    print i

print "\nYou were robbed of all of your possessions by a thief.";
inventory = []
if (not inventory):
    print "\nYou have nothing.";
else:
    print "\nYou have at least one item.\n";
You have  3  items.

Your items:
sword
armor
shield

You trade your sword for a battle axe.

Your items:

battle axe
armor
shield

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

Your shield is destroyed in a fierce battle.

Your items:
battle axe
armor

You were robbed of all of your possessions by a thief.

You have nothing.

example 4.2 page no : 124

In [2]:
inventory = []
inventory.append("sword");
inventory.append("armor");
inventory.append("shield");
print "You have " , len(inventory) , " items.";
print "\nYour items:";
for i in inventory:
    print i

print "\nYou trade your sword for a battle axe.";
inventory[0] = "battle axe";
print "\nYour items:\n";
for i in inventory:
    print i

print "\nThe item name '" , inventory[0] , "' has ",
print len(inventory[0]) , " letters in it.";

print "You recover a crossbow from a slain enemy.";
inventory.insert(0,"crossbow");
print "Your items:";
for i in inventory:
    print i
    
print "\nYour shield is destroyed in a fierce battle.";
inventory.pop();
print "\nYour items:";
for i in inventory:
    print i
You have  3  items.

Your items:
sword
armor
shield

You trade your sword for a battle axe.

Your items:

battle axe
armor
shield

The item name ' battle axe ' has  10  letters in it.
You recover a crossbow from a slain enemy.
Your items:
crossbow
battle axe
armor
shield

Your shield is destroyed in a fierce battle.

Your items:
crossbow
battle axe
armor

example 4.3 page no : 132

In [4]:
print "Creating a list of scores.";
scores = []
scores.append(1500);
scores.append(3500);
scores.append(7500);
print "\nHigh Scores:";
for i in scores:
    print i

print "\nFinding a score.";

print "\nEnter a score to find: ";
score = 3500 #int(raw_input())
iter = score in scores
if (iter):
    print "Score found.";
else:
    print "Score not found.";

print "\nRandomizing scores.";
import random
for i in range(len(scores)):
    scores[i] = int(random.random() * 10000) 

print "\nHigh Scores:\n";
for i in scores:
    print i
    
print "\nSorting scores.";
scores.sort()
print "\nHigh Scores:\n";
for i in scores:
    print i
Creating a list of scores.

High Scores:
1500
3500
7500

Finding a score.

Enter a score to find: 
Score found.

Randomizing scores.

High Scores:

6658
5396
8522

Sorting scores.

High Scores:

5396
6658
8522

example 4.4 page no: 142

In [1]:
#setup
MAX_WRONG = 8;
#maximum number of incorrect guesses allowed
words = []  # collection of possible words to guess
words.append("GUESS");
words.append("HANGMAN");
words.append("DIFFICULT");
THE_WORD = words[0];
wrong = 0;
soFar = []
for i in range(len(THE_WORD)):
    soFar.append('-')

used = "";

print "Welcome to Hangman. Good luck!\n";
# main loop
while ((wrong < MAX_WRONG) and (''.join(soFar) != THE_WORD)):
    print "\n\nYou have " , (MAX_WRONG - wrong);
    print " incorrect guesses left.";
    print "\nYou've used the following letters:\n" , used 
    print "\nSo far, the word is:" , soFar
    print "\nEnter your guess: ";
    guess = raw_input()
    guess = guess.upper() #make uppercase since secret word in uppercase
    while guess in used:
        print "\nYou've already guessed " , guess 
        print "Enter your guess: ";
        guess = 'GUESS' #raw_input()
        guess = guess.upper()
    used += guess;
    
    if (guess in THE_WORD):
        print "That's right! " , guess , " is in the word.";
        #update soFar to include newly guessed letter
        for i in range(len(THE_WORD)):
            if (THE_WORD[i] == guess):
                soFar[i] = guess
    else:
        print "Sorry, " , guess , " isn't in the word.";
        wrong += 1
#shut down
if (wrong == MAX_WRONG):
    print "\nYou've been hanged!";
else:
    print "\nYou guessed it!";
print "\nThe word was " , THE_WORD 
Welcome to Hangman. Good luck!



You have  8
 incorrect guesses left.

You've used the following letters:


So far, the word is: ['-', '-', '-', '-', '-']

Enter your guess: 
e
That's right!  E  is in the word.


You have  8
 incorrect guesses left.

You've used the following letters:
E

So far, the word is: ['-', '-', 'E', '-', '-']

Enter your guess: 
ae
Sorry,  AE  isn't in the word.


You have  7
 incorrect guesses left.

You've used the following letters:
EAE

So far, the word is: ['-', '-', 'E', '-', '-']

Enter your guess: 
w
Sorry,  W  isn't in the word.


You have  6
 incorrect guesses left.

You've used the following letters:
EAEW

So far, the word is: ['-', '-', 'E', '-', '-']

Enter your guess: 
wae
Sorry,  WAE  isn't in the word.


You have  5
 incorrect guesses left.

You've used the following letters:
EAEWWAE

So far, the word is: ['-', '-', 'E', '-', '-']

Enter your guess: 
s
That's right!  S  is in the word.


You have  5
 incorrect guesses left.

You've used the following letters:
EAEWWAES

So far, the word is: ['-', '-', 'E', 'S', 'S']

Enter your guess: 
ews
Sorry,  EWS  isn't in the word.


You have  4
 incorrect guesses left.

You've used the following letters:
EAEWWAESEWS

So far, the word is: ['-', '-', 'E', 'S', 'S']

Enter your guess: 
h
Sorry,  H  isn't in the word.


You have  3
 incorrect guesses left.

You've used the following letters:
EAEWWAESEWSH

So far, the word is: ['-', '-', 'E', 'S', 'S']

Enter your guess: 
l
Sorry,  L  isn't in the word.


You have  2
 incorrect guesses left.

You've used the following letters:
EAEWWAESEWSHL

So far, the word is: ['-', '-', 'E', 'S', 'S']

Enter your guess: 
g
That's right!  G  is in the word.


You have  2
 incorrect guesses left.

You've used the following letters:
EAEWWAESEWSHLG

So far, the word is: ['G', '-', 'E', 'S', 'S']

Enter your guess: 
u
That's right!  U  is in the word.

You guessed it!

The word was  GUESS