Hangman Game
HANGMANPICS = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | /|\ | | | =========''', ''' +---+ | | O | /|\ | / | | =========''', ''' +---+ | | O | /|\ | / \ | | =========''']
words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat 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]
hangman0.py
import random ### variables for reading purpose only words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra'.split() #print(words) 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] print("a random word ",getRandomWord(words)) ## run this code 4 times and copy the words here ##>>> abc = 'casa puerta tierra arbol' ##>>> abcList = abc.split() ##>>> abcList ##['casa', 'puerta', 'tierra', 'arbol'] ## ##the split function turns a string with spaces into a list of words ## ##>>> abc = 'casa puerta tierra arbol' ##>>> abcList = abc.split() ##>>> abcList ##['casa', 'puerta', 'tierra', 'arbol'] ##>>> ln = len(abcList) ##>>> ln ##4 ##>>> import random ##>>> aNum = random.randint(0, len(abcList) - 1) ##>>> aNum ##3
Homework: study the displayBoard function the same way we did with getRandomWord
print('H A N G M A N') missedLetters = '' correctLetters = '' secretWord = getRandomWord(words) gameIsDone = False
Homework: Understand the program by using the strategies we already learn, tracing, flowchart, pseudocode or by taking the program apart into smaller pieces.