Hangman Python game help for MIT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eotness
    New Member
    • Oct 2016
    • 2

    Hangman Python game help for MIT

    Hi.

    I'm trying to do the Hangman assignment for an online MIT course. However, I'm running into problems with the master code:

    I've got the following code so far:

    Code:
    import urllib.request 
    
    # Hangman game
    #
    
    # -----------------------------------
    # Helper code
    # You don't need to understand this helper code,
    # but you will have to know how to use the functions
    # (so be sure to read the docstrings!)
    
    import random
    
    WORDLIST_FILENAME = "http://bribre.net/edX/MIT6001/words.txt"
    
    def loadWords():
        """
        Returns a list of valid words. Words are strings of lowercase letters.
    
        Depending on the size of the word list, this function may
        take a while to finish.
        """
        print("Loading word list from file...")
        # inFile: file
        #inFile = open(WORDLIST_FILENAME, 'r')
        inFile = urllib.request.urlopen(WORDLIST_FILENAME)
        # line: string
        line = inFile.readline()
        # wordlist: list of strings
        wordlist = line.split()
        print("  ", len(wordlist), "words loaded.")
        return wordlist
    
    def chooseWord(wordlist):
        """
        wordlist (list): list of words (strings)
    
        Returns a word from wordlist at random
        """
        return random.choice(wordlist)
    
    # end of helper code
    # -----------------------------------
    
    # Load the list of words into the variable wordlist
    # so that it can be accessed from anywhere in the program
    wordlist = loadWords()
    
    def isWordGuessed(secretWord, lettersGuessed):
        '''
        secretWord: string, the word the user is guessing
        lettersGuessed: list, what letters have been guessed so far
        returns: boolean, True if all the letters of secretWord are in lettersGuessed;
          False otherwise
        '''
        lettersGuessed = ['a', 'a', 's', 't', 'r', 'm', 'c']
        if letter in lettersGuessed:
          True
        else:
          False
        print(isWordGuessed(secretWord, lettersGuessed))
        return isWordGuessed
    
    
    
    def getGuessedWord(secretWord, lettersGuessed):
        '''
        secretWord: string, the word the user is guessing
        lettersGuessed: list, what letters have been guessed so far
        returns: string, comprised of letters and underscores that represents
          what letters in secretWord have been guessed so far.
        '''
        lettersGuessed = ['a', 'a', 's', 't', 'r', 'm', 'c']
        returns1 = '_a_ _';
        returns2 = '_ _c_';
        returns3 = 't_ _t';
        returns4 = 'ta_t';
        returns5 = 't_ct';
        print(getGuessedWord(secretWord, lettersGuessed))
        return getGuessedWord
    
    
    
    def getAvailableLetters(lettersGuessed):
        '''
        lettersGuessed: list, what letters have been guessed so far
        returns: string, comprised of letters that represents what letters have not
          yet been guessed.
        '''
        s = 'bdefghijklnopquvwxyz'
        lettersGuessed = ['a', 'a', 's', 't', 'r', 'm', 'c']
        returns = s
        print(getAvailableLetters(lettersGuessed))
        return getAvailableLetters
    
    def hangman(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. letter) per round.
    
        * The user should receive feedback immediately after each guess 
          about whether their guess appears in the computers word.
    
        * After each round, you should also display to the user the 
          partially guessed word so far, as well as letters that the 
          user has not yet guessed.
    
        Follows the other limitations detailed in the problem write-up.
        '''
        return isWordGuessed
        return guessedWord
        return getAvailableLetters
        return hangman
    
    
    
    
    # When you've completed your hangman function, uncomment these two lines
    # and run this file to test! (hint: you might want to pick your own
    # secretWord while you're testing)
    
    # secretWord = chooseWord(wordlist).lower()
    # hangman(secretWord)
    Of them, only the first function of the master code has any outputs:

    Code:
    Loading word list from file...
    ('  ', 55909, 'words loaded.')
    Can someone tell me what is the issue I'm running into? I know how to solve the problem as well as the solution, but trying to get there is virtually impossible. What I need to output is either a winning game or a losing game.

    I don't want the solution, just an indication of what's going wrong so I can deduce how to fix it.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Of them, only the first function of the master code has any outputs
    The second function, getAvailableLet ters(lettersGue ssed) returns a variable that has not been declared, i.e. return getAvailableLet ters. The third function, hangman(secretW ord), has 4 returns, and only the first one executes as the function then exits. Finally, you do not get any input from the user so there are no guesses to analyze. A tutorial is a good next step to learn the basics for any Python program https://wiki.python.org/moin/BeginnersGuide/Programmers

    Comment

    • eotness
      New Member
      • Oct 2016
      • 2

      #3
      I'll look into that link. Though I think I should be a bit more explicit regarding what the functions are:

      Function 1 is the loadWords function

      Function 2 is the chooseWord function

      Function 3 is the isWordGuessed function

      Function 4 is the getGuessedWord function

      Function 5 is the getAvailableLet ters function

      and Function 6 is the hangman function.

      I thought that getAvailableLet ters was declared (unless def doesn't actually declare a variable)?

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Just a small observation - if people cannot figure out your functions in a few seconds, you need to include a block of documentation explaining purpose, invocation, and return values.

        Trust me, for code that others must read, it is a huge bonus.

        Comment

        Working...