Part I: Assignment: Write a program to support the children\'s spelling game han
ID: 3532440 • Letter: P
Question
Part I: Assignment:
Write a program to support the children's spelling game hangman. Call the program hangman.py. Since there are many versions of hangman, be sure that you implement exactly the following.
The player is to guess the letters in a secret word. Use underscores to display the number of letters in the word. When the player guesses a correct letter, display the word with that letter showing. Correct guesses don't count against the player. Incorrect guesses count, and the player loses on the seventh incorrect guess. Each time the player is asked to enter a letter, the program should display how many guesses they have left and a list of all of the incorrect letters they have guessed. If the player accidentally chooses a letter that has already been guessed, this should not count as a guess. See sample games below.
Your program must contain at least the following functions. (If you want you may use other functions as well.)
Explanation / Answer
#!/usr/bin/python# Hangman Game
# imports import random
# constants HANGMAN = ( """ ---------- | | | | | | | | ------------- """, """ ---------- | | | O | | | | | ------------- """, """ ---------- | | | O | -+- | | | | ------------- """, """ ---------- | | | O | /-+- | | | | ------------- """, """ ---------- | | | O | /-+-/ | | | | ------------- """, """ ---------- | | | O | /-+-/ | | | | | ------------- """, """ ---------- | | | O | /-+-/ | | | | | | | | ------------- """, """ ---------- | | | O | /-+-/ | | | | | | | | | | ------------- """ )
MAX_WRONG = len(HANGMAN) - 1; WORDS = ("OVERUSED", "CLAM", "GUAM", "TAFFETA", "PYTHON")
# initialize variables word = random.choice(WORDS) so_far = "-" * len(word) wrong = 0 used = []
print("Welcome to Hangman. Good luck!")
while wrong < MAX_WRONG and so_far != word: print(HANGMAN[wrong]) print(" You've used the following letters: " + str(used)) print(" So far, the word is: " + so_far) guess = raw_input(" Enter your guess: ") guess = guess.upper()
while guess in used: print("You've already guessed the letter " + guess) guess = raw_input(" Enter your guess: ") guess = guess.upper()
used.append(guess)
if guess in word: print(guess + " is in the word!") new = "" for i in range(len(word)): if guess == word[i]: new += guess else: new += so_far[i]
so_far = new else: print(guess + " is not in the word!") wrong += 1
if wrong == MAX_WRONG: print(HANGMAN[wrong]) print(" You've been hanged!") else: print(" You guessed it!")
print(" The word was " + word)
raw_input(" Press the enter key to exit.")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.