[Tutor] Don't understand error messages.

WM. wferguson1 at socal.rr.com
Wed Apr 22 20:57:13 CEST 2009


Using Windows XP, Python 2.6 & Al Sweigart's "Invent Your Own...", I 
keyed in the Hangman Program. So far as I can tell it is totally proofed 
out.  Still, I get an error message which makes no sense to me.

Herewith, the program, followed by the error message.

# CONSTANTS  are  all CAPS.
# variables AreAllLikeThis
import random

HANGMANPIX = ['''
         ____
        |   |
        |  
        |
        |
        |
        | 
        | 
        | 
        |
===================''', '''


         ____
        |   |
        |   Q
        |
        |
        |
        | 
        | 
        | 
        |
===================''', '''


         ____
        |   |
        |   Q
        |  | |
        | 
        |
        | 
        | 
        | 
        |
===================''', '''


         ____
        |   |
        |   Q
        |  | |
        |  | |
        |   -
        | 
        | 
        | 
        |
===================''', '''


         ____
        |   |
        |   Q
        |  | |
        |  | |
        |  H-
        |  H
        |  H
        | 
        |
===================''', '''


         ____
        |   |
        |   Q
        |  | |
        |  | |
        |  H-H
        |  H H
        |  H H
        | 
        |
===================''', '''


         ____
        |   |
        |   Q
        | (| |
        | )| |
        |  H-H
        |  H H
        |  H H
        | 
        |
===================''', '''


         ____
        |   |
        |   Q
        | (| |)
        | )| |(
        |  H-H
        |  H H
        |  H H
        | 
        |
===================''', '''
         ____
        |   |
        |   Q
        | (| |)
        | )| |(
        | oH-H
        |  H H
        |  H H
        | 
        |
===================''', '''
        ____
        |   |
        |   Q
        | (| |)
        | )| |(
        | oH-Ho
        |  H H
        |  H H
        | 
        |
===================''', '''
         ____
        |   |
        |   Q
        | (| |)
        | )| |(
        | oH-Ho
        |  H H
        |  H H
        |  O
        |
===================''', '''
         ____
        |   |
        |   Q
        | (| |)
        | )| |(
        | oH-Ho
        |  H H
        |  H H
        |  O O
        |
==================='''] # line 151, equals line 96 in book.

Words = 'ant baboon badger bat bear beaver beetle birdamel cat\
clam cobra cougar coyote crab crane crow deerdog donkey duck\
eagle ferret fish fox frog goat goose hawk iguana jackal koala\
leech lemur lion lizard llama mite mole monkey moose moth mouse\
mule newt otter owl oyster panda parrot pigeon python quail rabbit\
ram rat raven rhino salmon seal shark sheep skunk sloth slug snail\
snake spider squid stork swan tick tiger toad trout turkey turtle\
wasp weasel whale wolf wombat worm zebra'.split()

def GetRandomWord(WordList):
    # This function returns a random string from the passed list of strings.
    WordIndex = random.randint (0, len(WordList) -1)
    return WordList [WordIndex]

def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord):
    print HANGMANPIX[len(MissedLetters)]
    print

    print 'MissedLetters:',
    for Letter in MissedLetters:
        print Letter,
        print

        Blanks = '_' * len(SecretWord)

        for i in range(len(SecretWord)):
            # 116 Replace Blanks with correctly guessed letters.
            if SecrettWord[i] in CorrectLetters:
                Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:]
               
    for Letters in Blanks:#Show SecretWord w 1 space between letters.
        print Letter,
    print

def GetGuess(AlreadyGuessed):# 124 Returns only a letter user entered.
    while True:
        print "Guess a letter."
        Guess = raw_input()#ppppppppppppppppppppppppppppppppppppppppppp
        Guess = Guess.lower()
        if len(Guess) != 1:
            print "Please enter a single letter."
        elif Guess in AlreadyGuessed:
            print "You used that letter, choose another."
        elif Guess not in 'abcdefghijklmnopqrstuvwxyz':
               print "Please enter a LETTER!"
        else:
            return Guess
#138
def PlayAgain():#Returns T/F for play/quit.
    print "Wanna do it again? Yes or NO?"
    return raw_input().lower().startswith('y')

print 'H A N G M A N' #145
MissedLetters = ''
CorrectLetters = ''
SecretWord = GetRandomWord(Words)
GameIsDone = False
# Let the user enter a letter.
while True:#151
    DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, 
SecretWord)                      
   
    Guess = GetGuess(MissedLetters + CorrectLetters)
   
    if Guess in SecretWord:
        CorrectLetters = CorrectLetters + Guess#book158...onscreen213
        # 160 Check if user has won.
        FoundAllLetters = True
        for i in range(len,(SecretWord)):
            if SecretWord[i] not in CorrectLetters:
                FoundAllLetters = False
                break
        if FoundAllLetters:
            print 'Yes! The secret word is "' + \
            SecretWord +'"! You have won!'
            GameIsDone = True
        else:
            MissedLetters = MissedLetters + Guess
            #171 Check for too many guesses.
            if len(MissedLetters) == len(HANGMANPIX) - 1:
                DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, 
SecretWord)
                print 'You have run out of guesses!\nAfter '\
                + str(len(CorrectLetters)) + " missed guesses and "\
                + str(len(CorrectLetters)) +\
                "correct guesses. The word was "' +  SecretWord  + "."'
                GameIsDone = True
#180 Ask user, "Go again?"
        if GameIsDone:
            if PlayAgain():
                MissedLetters = ''
                CorrectLetters = ''
                GameIsDone = False
                SecretWord = getRandomWord(Words)
            else:
                break

End of program, below is the error message:
       
Traceback (most recent call last):
  File "C:\Python26\SweigartHangMan.py", line 212, in <module>
    DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, SecretWord)
  File "C:\Python26\SweigartHangMan.py", line 183, in DisplayBoard
    for Letters in Blanks:#Show SecretWord w 1 space between letters.
UnboundLocalError: local variable 'Blanks' referenced before assignment
              

   

              
              
              
   

                   
                   
                            
                    
   
   
   
















More information about the Tutor mailing list