Write a python program that will let the user play Hangman as shown in the examp
ID: 3677716 • Letter: W
Question
Write a python program that will let the user play Hangman as shown in the examples below. The user will guess letters until either: They have correctly guessed all the letters in the word or they have incorrectly guessed 6 times. At each turn, display the player’s progress by showing the word they’re guessing in all capital letters, with all non-guessed characters replaced by dashes (“-”). Suggested Approach Here’s one possible way to approach this program: Prompt the user to enter a word Create a display string with the right number of hyphens (loop and add one for each letter in the target word). For each letter they guess, If the letter is not in the target word, count a miss Otherwise, make a new display string by loop through the target word’s letters if the letter matches their guess, add it to the new display string if the letter does not match their guess, copy whatever was in that spot of the old display string over replace the old display string with the new one If they have too many misses, tell them they lose; if there are no - left in the display string, tell them they win.
The specific output example format is shown in the examples below:
Enter a word: HANGMAN
[-------] You have 6 left, enter a letter: A
Correct!
[-A---A-] You have 6 left, enter a letter: B
Incorrect! [-A---A-] You have 5 left, enter a letter: C
Incorrect! [-A---A-] You have 4 left, enter a letter: D
Incorrect! [-A---A-] You have 3 left, enter a letter: E
Incorrect! [-A---A-] You have 2 left, enter a letter: F
Incorrect! [-A---A-] You have 1 left, enter a letter: G
Correct! [-A-G-A-] You have 1 left, enter a letter: K
You lose! The word was “HANGMAN”
ANOTHER EXAMPLE
Enter a word: COMPUTER
[--------] You have 6 left, enter a letter: E
Correct! [------E-] You have 6 left, enter a letter: B
Incorrect! [------E-] You have 5 left, enter a letter: C
Correct! [C-----E-] You have 5 left, enter a letter: A
Incorrect! [C-----E-] You have 4 left, enter a letter: P
Correct! [C--P--E-] You have 4 left, enter a letter: E
Correct! [C--P--E-] You have 4 left, enter a letter: R
Correct! [C--P--ER] You have 4 left, enter a letter: M
Correct! [C-MP--ER] You have 4 left, enter a letter: A
Incorrect! [C-MP--ER] You have 3 left, enter a letter: T
Correct! [C-MP-TER] You have 3 left, enter a letter: O
Correct! [COMP-TER] You have 3 left, enter a letter: U
You win! The word was “COMPUTER”
another example
Enter a word: Aa
[--] You have 6 left, enter a letter: a
You win! The word was “AA”
Explanation / Answer
hangmanWord = "" while len(hangmanWord) < 3 or len(hangmanWord) > 20 or not hangmanWord.isalpha(): hangmanWord = raw_input("Which word will your friend try to find? >>> ").lower() print "" if len(hangmanWord) < 3: print "ERROR: Please enter a word at least 3 letters long." if len(hangmanWord) > 20: print "ERROR: Please enter a word at most 20 letters long." if not hangmanWord.isalpha(): print "ERROR: Please only use letters in your word." triesLeft = 0 while not (triesLeft > 4 and triesLeft < 15): try: triesLeft = int(raw_input("How many incorrect letters can your friend try? (Recommended: 8-12) >>> ")) except ValueError: triesLeft = 0 print "" print "ERROR: Please insert a number between 5-15" letters = [] corrects = [] incorrects = [] for x in range(len(hangmanWord)): letters.append(hangmanWord[x:x+1]) corrects.append("_") for x in range(50): print "" guessedLetter = "" while not letters == corrects: underscore = "" for x in range(len(hangmanWord)): if corrects[x] == letters[x]: underscore = underscore + corrects[x] + " " else: underscore = underscore + "_ " print underscore print "" guessedLetter = raw_input("Guess a letter! >>> ").lower() guessedLetter.lower() print "" print "" if not len(guessedLetter) == 1 or not guessedLetter.isalpha(): print "Please input only a letter!" elif guessedLetter in incorrects: print "You already guessed this letter!" elif guessedLetter in letters: incorrects.append(guessedLetter) print "That's a correct letter!" for x in range(len(hangmanWord)): if letters[x] == guessedLetter: corrects[x] = (guessedLetter) elif triesLeft > 1: triesLeft -= 1 incorrects.append(guessedLetter) if triesLeft == 1: print "That is an incorrect letter! You have " + str(triesLeft) + " try left!" else: print "That is an incorrect letter! You have " + str(triesLeft) + " tries left!" else: print "" print "You lost! The word was " + hangmanWord exit() print "Letters you tried so far: " + str(incorrects) print "" print "You have found the word! The word was " + hangmanWordRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.