# Hangman game # # ----------------------------------- # Helper code # You don't need to understand this helper code, # but you will have to know how to use the functions # (so be sure to read the docstrings!) import random import string WORDLIST_FILENAME = "words.txt" def loadWords(): """ Returns a list of valid words. Words are strings of lowercase letters. Depending on the size of the word list, this function may take a while to finish. """ print("Loading word list from file...") # inFile: file inFile = open(WORDLIST_FILENAME, 'r') # line: string line = inFile.readline() # wordlist: list of strings wordlist = line.split() print(" ", len(wordlist), "words loaded.") return wordlist def chooseWord(wordlist): """ wordlist (list): list of words (strings) Returns a word from wordlist at random """ return random.choice(wordlist) # end of helper code # ----------------------------------- # Load the list of words into the variable wordlist # so that it can be accessed from anywhere in the program wordlist = loadWords() #signature def isWordGuessed(secretWord, lettersGuessed):#stub ''' secretWord: string, the word the user is guessing lettersGuessed: list, what letters have been guessed so far returns: boolean, True if all the letters of secretWord are in lettersGuessed; False otherwise ''' # FILL IN YOUR CODE HERE... #All letters in secret word are in letters guessed AllLetterinSecret = False #All letters in letters guessed are in secret word for letter in secretWord: if letter in lettersGuessed: AllLetterinSecret = True else: AllLetterinSecret = False break #for each letter in secret word: #if letter in lettersGuessed: #AllLetterinSecret = True #else AllLetterinSecret = False and break return AllLetterinSecret #print(isWordGuessed("rearranged", ["r", "e", "a", "n", "g", "e", "d"])) #print(isWordGuessed("tuff", ["t", "u", "f"])) #print(isWordGuessed("tuff", ["t", "u", "f", "a"])) #print(isWordGuessed("tuns", ["t", "u", "f"])) #In goes a string and then a list with single letter strings #Out goes a string def getGuessedWord(secretWord, lettersGuessed):#stub ''' secretWord: string, the word the user is guessing lettersGuessed: list, what letters have been guessed so far returns: string, comprised of letters and underscores that represents what letters in secretWord have been guessed so far. ''' wordDisplayed = '' prevletter = '' for letter in secretWord: if letter in lettersGuessed: wordDisplayed += letter else: if prevletter in lettersGuessed: wordDisplayed += '_' else: wordDisplayed += ' _' prevletter = letter # for pos, letter in enumerate(wordDisplayed): # if (pos != 0 and pos != len(wordDisplayed)-1) and letter == '_': # wordDisplayed = wordDisplayed[0:pos] + '_ ' + wordDisplayed[pos + 1:-1] return wordDisplayed #in goes list and out comes a string def getAvailableLetters(lettersGuessed): ''' lettersGuessed: list, what letters have been guessed so far returns: string, comprised of letters that represents what letters have not yet been guessed. ''' alpha = string.ascii_lowercase letteraviable = '' for letter in alpha: if not letter in lettersGuessed: letteraviable += letter return letteraviable # FILL IN YOUR CODE HERE... #input is string def hangman(secretWord): ''' secretWord: string, the secret word to guess. Starts up an interactive game of Hangman. * At the start of the game, let the user know how many letters the secretWord contains. * Ask the user to supply one guess (i.e. letter) per round. * The user should receive feedback immediately after each guess about whether their guess appears in the computers word. * After each round, you should also display to the user the partially guessed word so far, as well as letters that the user has not yet guessed. Follows the other limitations detailed in the problem write-up. ''' guessleft = 8 print("Welcome to the game, Hangman!") print("I am thinking of a word that is "+str(len(secretWord))+" letters long.") print("-----------") lettersGuessed = [] while True: print("You have " + str(guessleft) + " guesses left") print('Available Letters: ' +getAvailableLetters(lettersGuessed)) i = input("Please guess a letter: ") if not i in lettersGuessed: lettersGuessed.append(i) if i in secretWord: print("Good guess: " + getGuessedWord(secretWord, lettersGuessed)) else: guessleft -= 1 print("Oops! That letter is not in my word: " + getGuessedWord(secretWord, lettersGuessed)) else: print("Oops! You've already guessed that letter: " + getGuessedWord(secretWord, lettersGuessed)) if isWordGuessed(secretWord, lettersGuessed): print("-----------") print("Congratulations, you won!") break if guessleft == 0: print("-----------") print("Sorry, you ran out of guesses. The word was " + secretWord + ".") break print("-----------") hangman(chooseWord(wordlist)) # When you've completed your hangman function, uncomment these two lines # and run this file to test! (hint: you might want to pick your own # secretWord while you're testing) # secretWord = chooseWord(wordlist).lower() # hangman(secretWord)