Final Project Code in place

by Aparna S


import random LEXICON_FILE = "Lexicon.txt" # File to read word list from INITIAL_GUESSES = 8 # Initial number of guesses player starts with def play_game(secret_word): """ Add your code (remember to delete the "pass" below) """ word="" for i in range(0,len(secret_word)): word=word+"-" return word #return word def get_word(): """ This function returns a secret word that the player is trying to guess in the game. This function initially has a very small list of words that it can select from to make it easier for you to write and debug the main game playing program. In Part II of writing this program, you will re-implement this function to select a word from a much larger list by reading a list of words from the file specified by the constant LEXICON_FILE. """ file=open('Lexicon.txt') for line in file: word=line.strip() my_list=[] my_list.append(word) secret_word=random.choice(my_list) return secret_word def main(): """ To play the game, we first select the secret word for the player to guess and then play the game using that secret word. """ secret_word = get_word() dashes=play_game(secret_word) print(secret_word) length=len(secret_word) n=0 print(("The word now looks like this: ")+str(dashes)) print("You have "+str(INITIAL_GUESSES)+" guesses left") while n1: print("Please guess a single letter.") print("The word now looks like this: "+str(dashes)) print("You have "+str(INITIAL_GUESSES)+" guesses left") if letter in dashes: print("Already guesses that letter") n=n print("You have "+str(INITIAL_GUESSES-n)+ " guessses left.") else: for i in range(0, length): if letter==secret_word[i]: dashes=dashes[:i]+letter+dashes[i+1:] if "-" not in dashes: print("Congratulations. The word is: "+str(secret_word)) break if letter.isalpha() and len(letter)==1: if letter in secret_word: print("That guess is correct. The word is now "+dashes) if letter in dashes: n=n print("You have "+str(INITIAL_GUESSES-n)+ " guessses left.") else: n=n+1 print("You have "+str(INITIAL_GUESSES-n)+ " guessses left.") if letter not in secret_word and len(letter)==1: n=n+1 print("There are no "+str(letter)+"'s in this word") print("The word is now "+dashes) print("You have "+str(INITIAL_GUESSES-n)+ " guessses left.") if n==INITIAL_GUESSES: print("Sorry you lost. The secret word was "+str(secret_word)) if n==INITIAL_GUESSES and secret_word==dashes: print("Congratulations.The word is: "+str(secret_word)) This provided line is required at the end of a Python file to call the main() function. if name == "main": main()