MITx 6.00 Problem Set 3 Hangman game

Source: Internet
Author: User

All code See GITHUB:HTTPS://GITHUB.COM/PXJW/MITX-6.00.1-2-PROBLEM-SET/TREE/MASTER/6.00.1X/PROSET3Hangman part 1:is the WORD guessed?

Please read the Hangman Introduction before starting this problem. The helper functions you'll be creating in the next three exercises is simply suggestions them if want to get points for this hangman problem Set. If you ' d prefer to structure your hangman program in a different it, feel free to redo this problem Set in a different WA Y. However, if you're ' re new to programming, or at a loss of what to construct this program, we strongly suggest so you impl Ement the next three helper functions before continuing on to hangman Part 2.

We'll start by writing 3 simple functions that'll help us easily code the hangman problem. First, implement the function, isWordGuessed takes in, parameters-a string, secretWord and a list of letters, lettersGuessed . This function returns a boolean-true if have secretWord been guessed (ie, all the letters of IS in secretWord ) and lettersGuessed False oth Erwise.

Example Usage:

>>> Secretword = ' Apple ' >>> lettersguessed = [' E ', ' I ', ' k ', ' P ', ' r ', ' s ']>>> print Iswordgue Ssed (Secretword, lettersguessed) False

For the-this function, the assume of the letters in and is secretWord lettersGuessed lowercase.

defiswordguessed (Secretword, lettersguessed):" "secretword:string, the word the user is guessing lettersguessed:list, what letters has been guessed so far      Returns:boolean, True If all the letters of Secretword is in lettersguessed; False otherwise" "    #FILL in YOUR CODE here ...     forXinchSecretword:ifX not inchlettersguessed:returnFalsereturnTrue

PRINTING out the USER ' S GUESS

(5.0/5.0 points)

Next, implement the function that takes in the getGuessedWord parameters-a string, secretWord and a list of letters, lettersGuessed . This function returns a string, which is comprised of letters and underscores, based on what letters in is lettersGuessed secretWord . This shouldn ' t is too different from isWordGuessed !

Example Usage:

>>> Secretword = ' Apple ' >>> lettersguessed = [' E ', ' I ', ' k ', ' P ', ' r ', ' s ']>>> print Getguesse DWord (Secretword, lettersguessed) ' _ Pp_ E '

When inserting underscores into your string, it's a good idea-to-add at least a space after each one, so it's clear to the User how many unguessed letters is left on the string (compare the readability of ____ with _ _ _ _  ). This is called usability -it's very important, when programming, to consider the usability of your program. If users find your program difficult to understand or operate, they won ' t use it!

For the-problem, you is free-to-use spacing on any-wish-our grader would only check that the letters and unders Cores is in the proper order; It won't look at spacing. We do encourage the think about usability when designing.

For the-this function, the assume of the letters in and is secretWord lettersGuessed lowercase.

defGetguessedword (Secretword, lettersguessed):" "secretword:string, the word the user is guessing lettersguessed:list, what letters has been guessed so far Returns:string, comprised of letters and underscores that represents what letters in Secretword has been guessed    So far. " "    #FILL in YOUR CODE here ...String =""     forXinchSecretword:ifXinchlettersguessed:string+=xElse: String+='_'    returnString

PRINTING out all AVAILABLE LETTERS

(5.0/5.0 points)

Next, implement the function getAvailableLetters , takes in one parameter-a list of letters, lettersGuessed . This function returns a string the is comprised of lowercase Chinese letters-all lowercase 中文版 letters that was not in lettersGuessed .

Example Usage:

>>> lettersguessed = [' E ', ' I ', ' k ', ' P ', ' r ', ' s ']>>> print getavailableletters (lettersguessed) Abcdfghjlmnoqtuvwxyz

Note that this function should return the letters in alphabetical order, as in the example above.

For the-this function, your may assume and the letters in is lettersGuessed lowercase.

defgetavailableletters (lettersguessed):" "Lettersguessed:list, what letters has been guessed so far returns:string, comprised of letters that Represen    TS What letters has not yet been guessed. " "    #FILL in YOUR CODE here ...notguessed = []    #notguessed = String.ascii_lowercase     forXinchRange (26): notguessed+ = Chr (x + ord ('a'))     forYinchlettersGuessed:notGuessed.remove (y) string=""     forZinchnotguessed:string+=ZreturnString

Hangman Part 2:the GAME

(15.0/15.0 points)

Now you'll implement the function hangman , which takes one parameter-the the user is to secretWord guess. This starts up an interactive game of hangman between the user and the computer. Be sure-advantage of the three helper functions,,, and, that's you isWordGuessed getGuessedWord getAvailableLetters ' ve defined in the previous par T.

Hints:
  • You should start by noticing where we ' re using the provided functions (at the top of ps3_hangman.py ) to load the words and pick a R Andom one. Note that the functions loadWords chooseWord and should only is used on your local machine, not in the tutor. When you enter into your solution in the tutor, you are only need to give your hangman function.

  • Consider using to lower() convert the user input to lower case. For example:

    Guess = ' A ' guessinlowercase = Guess.lower ()
  • Consider writing additional helper functions if you need them!

  • There is four important pieces of information the wish to store:

    1. secretWord: The word to guess.
    2. lettersGuessed: The letters that has been guessed so far.
    3. mistakesMade: The number of incorrect guesses made so far.
    4. availableLetters: The letters is still be guessed. Every time a player guesses a letter, the guessed letter must is removed fromavailableLetters(And if they guess a letter that's not inavailableLetters, you should print a message telling them they ' ve already guessed that-so try again!).
      defHangman (Secretword):" "secretword:string, the secret word to guess.    Starts up an interactive game of hangman.    * At the start of the game, let the user know how many letters the Secretword contains.    * Ask the user to supply one guess (i.e) per round. * The user should receive feedback immediately after each guess about whether their guess appears in the Computers W    Ord. * After all round, you should also display to the user the partially guessed word so far, as well as letters that T    He user has not yet guessed.    Follows the other limitations detailed in the problem write-up. " "    #FILL in YOUR CODE here ...    Print("Welcome to the game hangman!")    Print("I am thinking of a word is"+ STR (len (secretword)) +"Letters Long")    Print("-----------") lettersguessed=[] Guesses= 8#While all the letters of Secretword was not yet in lettersguessed and guesses-is more than 0     while  notIswordguessed (Secretword, lettersguessed) andGuesses >0:#Print First line        Print(" you have"+ STR (guesses) +"guesses Left")        #Print Second line        Print("Available Letters:"+getavailableletters (lettersguessed))#Print Third line        #if user input a letter that have already been entered, reprompt for letter         whileTrue:guessletter= Raw_input ("Please guess a letter:"). Lower ()ifGuessletterinchlettersguessed:Print("oops! You ' ve already guessed this letter:"+Getguessedword (Secretword, lettersguessed))Print("-----------")                Print(" you have"+ STR (guesses) +"guesses Left")                Print("Available Letters:"+getavailableletters (lettersguessed))Else:                 Breaklettersguessed+=Guessletter#Print last line        #if correctly guessed secret word        ifiswordguessed (Secretword, lettersguessed):Print("Good guess:"+Getguessedword (Secretword, lettersguessed))Print("-----------")            Print("Congratulations, you won!")             Break        #else if the Guess letter was in secret word        elifGuessletterinchSecretword:Print("Good guess:"+Getguessedword (Secretword, lettersguessed))Print("-----------")        #Else The Guess letter was not in secret word        Else:            Print("oops! That's not in my word:"+Getguessedword (Secretword, lettersguessed))Print("-----------") guesses-= 1#if ran out of guesses        ifguesses = =0:Print("Sorry, you ran out of guesses. The word was"+ Secretword +".")

MITx 6.00 Problem Set 3 Hangman game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.