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

Write a simple version of the classic game Hangman in python. As the heart of yo

ID: 3628498 • Letter: W

Question

Write a simple version of the classic game Hangman in python. As the heart of your program, implement a function playHangman(secret) that manages a single round of the game. The parameter is presumed to be a string which is the secret answer; consider it unknown to the player.
The game should begin by displaying a version of the answer with the original spacing and punctuation, but with each alphabetic character replaced by an underscore '_' character. Then start asking the player to enter a single character as a guess. If that character is hidden in the answer, inform the user of his success and redisplay the disguised message but with all occurrences of the letter displayed. If the guess was wrong, inform the user. This process should continue either until the entire message had been uncovered or until there have been seven incorrect guesses.

Explanation / Answer

Dear,
import random
import time
#hangman game
 #the words - hints (this is a list of tuples with the word and corresponding hint
 theWords = [("kitchen", "some place that you cook in"),
 ("house", "its a place that you live in"),
 ("friend", "it is someone really close to you")]
 #seeding and finding out the random word and hint
random.seed(time.time())
 x = random.randint(0, len(theWords) - 1)
finalWord = theWords[x][0]
finalHint = theWords[x][1]
 #setting up the amount of incorrectGuesses the user has and the word guessed so far
incorrectGuesses = 5
soFar = "-" * len(finalWord)
#main while loop 
 while soFar != finalWord and incorrectGuesses > 0:
 print "You have %d many guesses left" % incorrectGuesses
 userGuess = raw_input("Please input ONE character or 'exit' to quit or 'hint' to get the hint: ")
 userGuess = userGuess.lower()
 #checking to see if the user wants to exit or get a hint
    if userGuess == "exit":
        break
    elif userGuess == "hint":
        print finalHint
 userGuess = raw_input("Please input ONE character or 'exit' to quit or 'hint' to get the hint: ")
    
 #checks to see if the user's guess is in the word and if it is update so far  
    if userGuess in finalWord:
 index = finalWord.find(userGuess)
        x = list(soFar)
        x[index] = userGuess
        soFar = "".join(x)
 print "Yes that guess is in the word"
 #the user didnt guess a correct letter and incorrectguess is reduced    
    else:
 print "Im sorry that letter is not in the word."
 incorrectGuesses = incorrectGuesses - 1
if soFar == finalWord:
    print "Congrats you won!"
else:
    print "Thanks for playing"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote