Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**Need a solution in python 2.7. I have also attached the code I wrote for the c

ID: 3707442 • Letter: #

Question

**Need a solution in python 2.7. I have also attached the code I wrote for the computer to generate a random word which it reads from a txt file and generates a set of blanks representing the number of letters in the random word. That was question 1, we have to use that randomly generated word in our word game.

In this exercise we want to implement a word game. The computer should randomly choose a word and the player is supposed to guess the word. Your program just give the number of letters as the clue to the user, then user starts guessing the letters. If the letter is used in the word, program show the location(s) of the letter. Before asking for another letter guess, program should ask the user if she/he wants to guess the word (maybe the user already guessed the word). The program should continue until the user gets the correct word or guesses all letters. At then show how many attempts the user made.

The following is an interaction example (assume the word is “mission”):

Welcome to the word game!
_ _ _ _ _ _ _

Would like to guess the word (no/yes)? no

Guess a letter? a

_ _ _ _ _ _ _

Would like to guess the word (no/yes)? no

Guess a letter? m

m _ _ _ _ _ _

Would like to guess the word (no/yes)? no

Guess a letter? e

m _ _ _ _ _ _

Would like to guess the word (no/yes)? no

Guess a letter? i

mi _ _ i _ _

Would like to guess the word (no/yes)? no

Guess a letter? s

missi _ _

Would like to guess the word (no/yes)? yes

Guess a word?

Missing

Wrong guess

Would like to guess the word (no/yes)? no

Guess a letter? n

missi _ n

Would like to guess the word (no/yes)? yes

Guess a word?

mission

Congratulations!
8 attempts!

import random
def read_File(filename):
fhand = open(filename)
List = []
for i in fhand:
i = i.strip()
List.append(i)
return List

  
randomWord = (random.choice(read_File("words.txt")))
randWord = randomWord.lower()

blanks = '_ ' * len(randomWord)
print 'Welcome to Guess the word game!:', blanks

****Sample words from from txt file "words.txt"

ABA
ABAC
ABACA
ABACAS
ABACI
ABACK
ABACS
ABACTERIAL
ABACTINAL
ABACTINALLY
ABACTOR
ABACTORS
ABACUS
ABACUSES
ABAFT
ABAKA
ABAKAS

Explanation / Answer

ScreenShot

-----------------------------------------------------------------------------------------------------------------------

Program

#import header files
from pip import index
import random;
import collections
#read random word from the word file
randomWord=list(random.choice(open("C:\Users\deept\Desktop\words.txt").readline().split()))
#create a blank list
blanks ='_ ' * len(randomWord)
#main method definition
def main():
    #variable for looping
    val="true"
    # variable for matching the character's
    length=0
    randomLength=len(randomWord)
    #number of attempts variable
    numAttempt=0
    #welcome message
    print("Welcome to the word game!")
    print("_ _ _ _ _ _ _")
    #loop to guess the word
    while val=="true":
        #user choice prompt
        userChoice=input("Would like to guess the word (no/yes)? ")
        #increment count of number of attempts each time
        numAttempt=numAttempt+1
        #if choice "no" then ask for letter otherwise word
        if(userChoice=="no"):
            ch=input("Guess a letter?")
            #loop through the search
            for i in randomWord:
                #compare each random word character with user entered character
                if i==ch.upper():
                    #increment count to match
                    length=length+1
                    #getting index
                    j=randomWord.index(i)
                    #replace with character
                    blanks[j].replace('_',i)
                    #print that string
                    print(blanks)
                else:
                    continue
            #ask for word      
        else:
            word=input("Guess a word?").upper()
            wordList=list(word)
            #word matching and display congrats if it matches
            if randomWord==wordList:
                str2 = ''.join(word)
                print(str2)
                print("congradulation")
                print(numAttempt,"attempts")
                val="false"
                #else display wrong guess and continue
            else:
                str3 = ''.join(word)
                print(str3)
                print("Wrong guess")
                continue
        #check length and print congrats
        if length==randomLength:
            print("congradulation")
            print(numAttempt,"attempts")
            val="false"
        else:
            continue

#main program
if __name__=="__main__":
    main()