Write python program game of hangman. In this game, the player has a limited num
ID: 3727570 • Letter: W
Question
Write python program game of hangman.
In this game, the player has a limited number of turns in which to guess all the letters of a mystery word. Your program must have the following features:
A randomly-selected mystery word from w.txt.
import random, then call random.randint(1,110000) in order to randomly select a number between 1 and the total number of lines in w.txt.
10 turns (unique letter guesses) for the player
A display (each turn) of the correctly guessed letters, in all their correct positions within the mystery word.
A display (each turn) of the incorrectly guessed letters.
A success message if the user wins.
A failure message if the user loses (uses up 10 turns without guessing all the letters of the words).
A message if the user accidentally re-enters an already-guessed letter, whether that letter was correct or not. Note that the user gets this message whether the already-guessed letter was right or wrong. Also, any repeated guesses should not be counted towards the 10-turn max.
Explanation / Answer
Screenshot:
Code:
# Header File
import random
# Read the lines randomly from the file
lines = open('C:/Users/deept/Desktop/w.txt').read().splitlines()
print(myline)
# Control variable for 10 guesses
k=0
# Array for storing already guessed values
guesslist=[]
# Loop for checking 10 guesses
while k<10:
# Temp varaible to store when found
temp=-1
count=0
ctr=0
guessCtr=0
found=False
guessfound=False
# Take the guess
guess=input("Enter user guess as a letter a-z :")
# while loop to check if duplicate guess
while guessfound == False and guessCtr < len(guesslist) :
#if logic to handle found and not found cases
if(guess==guesslist[guessCtr]):
guessfound=True
guessCtr=guessCtr + 1
else:
guessCtr=guessCtr+1
if guessfound==True:
print("Duplicate Guess entered ",guess)
else:
# add to the guess list
guesslist.append(guess)
# loop to scan the random line with teh guessed value
while found == False and ctr < len(myline) :
#if logic to handle found and not found cases
if(guess==myline[ctr]):
temp=ctr
found=True
ctr=ctr + 1
count=count+1
else:
ctr=ctr+1
if found==True:
print("Found in ",temp)
else:
print("Not Found ",guess)
k=k+1
# If logic to print result after all guesses
if count==10:
print ("Hurrai !!!!!!, you win")
else:
print ("Better luck next time , you lose")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.