# # H A N G M A N # # # PUT PARTNER NAMES HERE # from random import choice from hangman_lib import * def addToList(alist, word): """Adds word to alist only if it isn't already in the list""" word = word.strip('\'".-,():;?!') if word != '' and word not in alist: alist.append(word) def replaceLetter(letter, word, wordSoFar): """ Letter is the letter the person guessed word is the secret word they are trying to guess wordSoFar is what they have guessed so far (it is twice as long as word because there are spaces between the letters. If letter is in word this function adds the letter to wordSoFar in the correct spot""" wordSoFarList = list(wordSoFar) for i in range(len(word)): if letter == word[i]: wordSoFarList[i * 2] = letter return ''.join(wordSoFarList) def get_random_word(): file = open('word_list.txt') words = [] for line in file: for word in line.split(): addToList(words, word) file.close() return(choice(words)) def greeting(): """Display greeting and give instructions if necessary""" print("Welcome to Hangman!!! coded by <>") response = input("\nHave you played Hangman before (y/n)? ") if response == 'n': # it's their first time -- let's give them instructions. print( "The objective of Hangman is to guess a secret word letter by letter.") print( "If you guess a letter in the word, we'll show you that letter.") print( "But if you guess wrong, we'll draw part of the hangman's body.") print( "Don't let his whole body get drawn, or else you lose!\n") print ("Great, so you're ready to play.") print ("\nGood luck!\n") print ("[Press enter when ready to play.]") input() # this just waits for them to press enter... they can type # other stuff but it doesn't affect anything. def createBlankWord(length): """Will create a word consisting entirely of underlines (each underline is separated by a space). For example createBlankWord(5) will return _ _ _ _ _ """ underlineString = '' for i in range(length): underlineString += '_ ' return(underlineString) def addBlanks(word): """Since the word the person is building up has spaces (e.g., _ _ _ O O _ and S C H O O L) and the word they are trying to guess does not (e.g. SCHOOL) a simple if guessWord == secretWord does not work so I am using guessWord == addBlanks(secretWord) which returns true if they represent the same word and false otherwise""" #print (word) newWord = '' for ch in word: newWord += ch + ' ' return(newWord) ## ## ## M A I N H A N G M A N F U N C T I O N ## def hangman(): print("TODO: write this function!")