Answered step by step
Verified Expert Solution
Link Copied!

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, letter-by-letter.Game play should (eventually) look like this:Enter the secret word: LETTERS------------------------------------------------------------Word so far: _______Misses: 0What letter would you like to guess? EWord so far: _E__E__Misses: 0What letter would you like to guess? RWord so far: _E__ER_Misses: 0What letter would you like to guess? AWord so far: _E__ER_Misses: 1What letter would you like to guess? TWord so far: _ETTER_Misses: 1What letter would you like to guess? SWord so far: _ETTERSMisses: 1What letter would you like to guess? BWord so far: _ETTERSMisses: 2What letter would you like to guess? QWord so far: _ETTERSMisses: 3What 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 word(s) 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 "
"*60In sample runs, this will be represented with a row of dashes. (You really don't want to scroll down through 60 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 word/words that the first user enters (secret).The displayed version of the secret (display). This will be what the second user sees as they are guessing. It will start with all underscores (e.g.____), be filled in with letters as they are guessed (e.g._M_T), and for a winning game, will end up the same as the original secret entered by the first user (e.g. CMPT)The 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 get_secret that asks the user for the secret word/phrase, clears the screen, and returns the secret string.def get_secret(): """ Get the secret word/phrase."""In the main part of the program, call the get_secret function and store the result in secret. Create a string display that contains len(secret) 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 do_turn 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 letter).def do_turn(display, misses): """ Display the current status for the user and let them make a guess. """Don't 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 new_display 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 (i.e. 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 = new_display()Here are some examples of calling this function, and the values it should return in each case:new_display("SECRET","_E__E_","C")=="_EC_E_",1new_display("SECRET","_E__E_","Q")=="_E__E_",0new_display("ABBA","A__A","B")== "ABBA", 2Hint: 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 (i.e. it's partially complete, but not totally finished after filling in the blanks):def new_display(word, display, letter): newdisp ="" for i in range(len(word)): 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 do_turn to do the part of the visible to the user, and get the letter they want to guess.Call new_display 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 control-C to stop your program.After the loop, you should print out an appropriate win/loss 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions

Question

What steps are involved with agreeing to terms with a seller?

Answered: 1 week ago

Question

Why is pricing one of the key global strategic decisions?

Answered: 1 week ago