The Assignment -- Your assignment is to design and write a computer program to p
ID: 3696276 • Letter: T
Question
The Assignment -- Your assignment is to design and write a computer program to play Hangman. In particular, your program should do the following: Read the file dictionary.txt, which contains the full contents of the Official Scrabble Player's Dictionary, Second Edition. This file has over 120,000 words, which should be more than enough for our purposes. You should use a Python dictionary to store the words such that each key is the word length with it corresponding value being a list of all word of that length (for example 3: ['dog', 'cat', 'the',...]) Prompt the user for a word length. Reprompting as necessary until she enters a valid choice. For example, enters a number such that there's at least one word that's exactly that long (i.e., don't accept word lengths of -42 or 137, since no English words are that long) Choose a secret word of the correct length at random Prompt the user for a number of guesses, which must be an integer greater than zero. Don't worry about unusually large numbers of guesses - after all, having more than 26 guesses is clearly not going to help your opponent! Play a game of Hangman using the standard Hangman rules, as described below: Print out how many guesses the user has remaining, along with any letters the player has guessed and the current blanked-out version of the word. Prompt the user for a single letter guess, reprompting until the user enters a letter that she hasn't guessed yet. Make sure that the input is exactly one character long and that it's a letter of the alphabet. Reveal the position of the guessed letter (if any) to the user. If the word doesn't contain any copies of the letter, subtract a remaining guess from the user. If the player has run out of guesses, display the word that the computer "picked". If the player correctly guesses the word, congratulate her.Explanation / Answer
Hi below i have written a sample class for Hangman in Python for your reference,I might missed some scenario but hope this code helps you understand the concept :)
class Hangman():
def __init__(self): print "Welcome to 'Hangman', are you ready to die?" print "(1)Yes, for I am already dead. (2)No, get me outta here!" user_choice_1 = raw_input("->") if user_choice_1 == '1': print "Loading nooses, murderers, rapists, thiefs, lunatics..." self.start_game() elif user_choice_1 == '2': print "Bye bye now..." exit() else: print "I'm sorry, I'm hard of hearing, could you repeat that?" self.__init__() def start_game(self): print "A crowd begins to gather, they can't wait to see some real" print "justice. There's just one thing, you aren't a real criminal." print "No, no. You're the wrong time, wrong place type. You may think" print "you're dead, but it's not like that at all. Yes, yes. You've" print "got a chance to live. All you've gotta do is guess the right" print "words and you can live to see another day. But don't get so" print "happy yet. If you make 6 wrong guess, YOU'RE TOAST! VAMANOS!" self.core_game() def core_game(self): guesses = 0 letters_used = "" the_word = "pizza" progress = ["?", "?", "?", "?", "?"] while guesses < 6: guess = raw_input("Guess a letter ->") if guess in the_word and not in letters_used: print "As it turns out, your guess was RIGHT!" letters_used += "," + guess self.hangman_graphic(guesses) print "Progress: " + self.progress_updater(guess, the_word, progress) print "Letter used: " + letters_used elif guess not in the_word and not(in letters_used): guesses += 1 print "Things aren't looking so good, that guess was WRONG!" print "Oh man, that crowd is getting happy, I thought you" print "wanted to make them mad?" letters_used += "," + guess self.hangman_graphic(guesses) print "Progress: " + "".join(progress) print "Letter used: " + letters_used else: print "That's the wrong letter, you wanna be out here all day?" print "Try again!" def hangman_graphic(self, guesses): if guesses == 0: print "________ " print "| | " print "| " print "| " print "| " print "| " elif guesses == 1: print "________ " print "| | " print "| 0 " print "| " print "| " print "| " elif guesses == 2: print "________ " print "| | " print "| 0 " print "| / " print "| " print "| " elif guesses == 3: print "________ " print "| | " print "| 0 " print "| /| " print "| " print "| " elif guesses == 4: print "________ " print "| | " print "| 0 " print "| /| " print "| " print "| " elif guesses == 5: print "________ " print "| | " print "| 0 " print "| /| " print "| / " print "| " else: print "________ " print "| | " print "| 0 " print "| /| " print "| / " print "| " print "The noose tightens around your neck, and you feel the" print "sudden urge to urinate." print "GAME OVER!" self.__init__() def progress_updater(self, guess, the_word, progress): i = 0 while i < len(the_word): if guess == the_word[i]: progress[i] = guess i += 1 else: i += 1 return "".join(progress) game = Hangman()Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.