Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part II: Programming For this assignment, your job is to create a Hangman game played by two players in hangman.py . For those who have
Part II: Programming
For this assignment, your job is to create a Hangman game played by two players in hangman.py For those who have never played Hangman, the rules are simple. One player thinks of a secret word or phrase, and the other tries to guess it letterbyletter.Game play should eventually look like this:Enter the secret word: LETTERSWord so far: Misses: What letter would you like to guess? EWord so far: EEMisses: What letter would you like to guess? RWord so far: EERMisses: What letter would you like to guess? AWord so far: EERMisses: What letter would you like to guess? TWord so far: ETTERMisses: What letter would you like to guess? SWord so far: ETTERSMisses: What letter would you like to guess? BWord so far: ETTERSMisses: What letter would you like to guess? QWord so far: ETTERSMisses: What letter would you like to guess? LYou guessed the secret correctly: LETTERSThere is another page of sample runs that you can look at as well.There is no general way to clear the screen in Python, but you don't want to leave the secret words on the screen when the guessing starts. The easiest way to do this is to scroll the secret off the screen. You can do this by printing a bunch of line breaks:print
In sample runs, this will be represented with a row of dashes. You really don't want to scroll down through blank lines, do you?Creating the ProgramHere are the things that you need to keep track of in the main part of the program:The secret wordwords that the first user enters secretThe displayed version of the secret display This will be what the second user sees as they are guessing. It will start with all underscores eg be filled in with letters as they are guessed egMT and for a winning game, will end up the same as the original secret entered by the first user eg CMPTThe number of incorrect guesses misses that the user has made.The number of letters that are still unguessed in the secret.Whatever else you need.For this program, you must break the task up into functions. Here is a guide to creating the program for this problem:Start by creating a function getsecret that asks the user for the secret wordphrase clears the screen, and returns the secret string.def getsecret: Get the secret wordphraseIn the main part of the program, call the getsecret function and store the result in secret. Create a string display that contains lensecret underscores.The string display will be what we show the user as they are guessing. The underscores will be replaced with letters as they are guessed.Create a variable to count the number of incorrect guesses the user has made misses and initialize it appropriately. You will also need to keep track of the number of unguessed letters left in the secret: this along with the count of the misses will be used to decide when the game is over.Create a function doturn that takes two arguments: the display string, and the number of misses made so far. This function should do the part of the turn the user sees display the part of the secret they have guessed, display the number of misses, and ask them to guess a letterdef doturndisplay misses: Display the current status for the user and let them make a guess. Dont worry about the error checking exactly one letter The function should return the letter the user enters.Once we have both the secret word, and a guess, we need to be able to update the display string, and keep track of the number of letters discovered. Create a function newdisplay that takes three arguments: the secret, the display string, and the letter the user guessed.Once it has these values, the function can calculate the new value for the display string ie replace all of the underscores where the letter the user guessed is the letter While it does this, it can count the replacements.Both of those values the new display string string, and the replacement count should be returned. A Python function can return multiple values like this:return newdisp, countThen, you can call the function and capture both return values like this:display, count newdisplayHere are some examples of calling this function, and the values it should return in each case:newdisplaySECRETEECECEnewdisplaySECRETEEQEEnewdisplayABBAAAB "ABBA", Hint: Create a for loop that examines the characters in the old display string and the secret string: the characters in each position should correspond For each one, either copy it to the new display string, or replace it with the guessed letter. Here's a partial function ie it's partially complete, but not totally finished after filling in the blanks:def newdisplayword display, letter: newdisp for i in rangelenword: if letter: newdisp newdisp letter else: newdisp newdisp return newdisp, In the main part of your program, create the main loop for the game. In it you should:Call doturn to do the part of the visible to the user, and get the letter they want to guess.Call newdisplay to update the display string and get the number of occurrences of the letter.Update the variables holding the number of misses and the number of unguessed letters as appropriate.If you do these things correctly, you should have everything you need to create a condition for this loop. The loop should continue when there are both letters remaining to be guessed and less than six misses have been made.As you're working on your program, you may find that your loop doesn't exit properly. Press controlC to stop your program.After the loop, you should print out an appropriate winloss message as seen in the sample runs.Add code where appropriate to make sure the user enters exactly one character when prompted for a guess. Display an error message if they don't.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started