473,385 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. import urllib.request 
  2.  
  3. # Hangman game
  4. #
  5.  
  6. # -----------------------------------
  7. # Helper code
  8. # You don't need to understand this helper code,
  9. # but you will have to know how to use the functions
  10. # (so be sure to read the docstrings!)
  11.  
  12. import random
  13.  
  14. WORDLIST_FILENAME = "http://bribre.net/edX/MIT6001/words.txt"
  15.  
  16. def loadWords():
  17.     """
  18.     Returns a list of valid words. Words are strings of lowercase letters.
  19.  
  20.     Depending on the size of the word list, this function may
  21.     take a while to finish.
  22.     """
  23.     print("Loading word list from file...")
  24.     # inFile: file
  25.     #inFile = open(WORDLIST_FILENAME, 'r')
  26.     inFile = urllib.request.urlopen(WORDLIST_FILENAME)
  27.     # line: string
  28.     line = inFile.readline()
  29.     # wordlist: list of strings
  30.     wordlist = line.split()
  31.     print("  ", len(wordlist), "words loaded.")
  32.     return wordlist
  33.  
  34. def chooseWord(wordlist):
  35.     """
  36.     wordlist (list): list of words (strings)
  37.  
  38.     Returns a word from wordlist at random
  39.     """
  40.     return random.choice(wordlist)
  41.  
  42. # end of helper code
  43. # -----------------------------------
  44.  
  45. # Load the list of words into the variable wordlist
  46. # so that it can be accessed from anywhere in the program
  47. wordlist = loadWords()
  48.  
  49. def isWordGuessed(secretWord, lettersGuessed):
  50.     '''
  51.     secretWord: string, the word the user is guessing
  52.     lettersGuessed: list, what letters have been guessed so far
  53.     returns: boolean, True if all the letters of secretWord are in lettersGuessed;
  54.       False otherwise
  55.     '''
  56.     lettersGuessed = ['a', 'a', 's', 't', 'r', 'm', 'c']
  57.     if letter in lettersGuessed:
  58.       True
  59.     else:
  60.       False
  61.     print(isWordGuessed(secretWord, lettersGuessed))
  62.     return isWordGuessed
  63.  
  64.  
  65.  
  66. def getGuessedWord(secretWord, lettersGuessed):
  67.     '''
  68.     secretWord: string, the word the user is guessing
  69.     lettersGuessed: list, what letters have been guessed so far
  70.     returns: string, comprised of letters and underscores that represents
  71.       what letters in secretWord have been guessed so far.
  72.     '''
  73.     lettersGuessed = ['a', 'a', 's', 't', 'r', 'm', 'c']
  74.     returns1 = '_a_ _';
  75.     returns2 = '_ _c_';
  76.     returns3 = 't_ _t';
  77.     returns4 = 'ta_t';
  78.     returns5 = 't_ct';
  79.     print(getGuessedWord(secretWord, lettersGuessed))
  80.     return getGuessedWord
  81.  
  82.  
  83.  
  84. def getAvailableLetters(lettersGuessed):
  85.     '''
  86.     lettersGuessed: list, what letters have been guessed so far
  87.     returns: string, comprised of letters that represents what letters have not
  88.       yet been guessed.
  89.     '''
  90.     s = 'bdefghijklnopquvwxyz'
  91.     lettersGuessed = ['a', 'a', 's', 't', 'r', 'm', 'c']
  92.     returns = s
  93.     print(getAvailableLetters(lettersGuessed))
  94.     return getAvailableLetters
  95.  
  96. def hangman(secretWord):
  97.     '''
  98.     secretWord: string, the secret word to guess.
  99.  
  100.     Starts up an interactive game of Hangman.
  101.  
  102.     * At the start of the game, let the user know how many 
  103.       letters the secretWord contains.
  104.  
  105.     * Ask the user to supply one guess (i.e. letter) per round.
  106.  
  107.     * The user should receive feedback immediately after each guess 
  108.       about whether their guess appears in the computers word.
  109.  
  110.     * After each round, you should also display to the user the 
  111.       partially guessed word so far, as well as letters that the 
  112.       user has not yet guessed.
  113.  
  114.     Follows the other limitations detailed in the problem write-up.
  115.     '''
  116.     return isWordGuessed
  117.     return guessedWord
  118.     return getAvailableLetters
  119.     return hangman
  120.  
  121.  
  122.  
  123.  
  124. # When you've completed your hangman function, uncomment these two lines
  125. # and run this file to test! (hint: you might want to pick your own
  126. # secretWord while you're testing)
  127.  
  128. # secretWord = chooseWord(wordlist).lower()
  129. # hangman(secretWord)
  130.  
  131.  
Of them, only the first function of the master code has any outputs:

Expand|Select|Wrap|Line Numbers
  1. Loading word list from file...
  2. ('  ', 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.
Oct 6 '16 #1
3 2068
dwblas
626 Expert 512MB
Of them, only the first function of the master code has any outputs
The second function, getAvailableLetters(lettersGuessed) returns a variable that has not been declared, i.e. return getAvailableLetters. The third function, hangman(secretWord), 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
Oct 6 '16 #2
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 getAvailableLetters function

and Function 6 is the hangman function.

I thought that getAvailableLetters was declared (unless def doesn't actually declare a variable)?
Oct 6 '16 #3
Oralloy
987 Expert 512MB
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.
Oct 8 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Pete Shinners | last post by:
Recently I conducted a short interview with Steve Moret, the Lead Programmer for "Greyhawk: Temple of Elemental Evil". Python was used for the scripting in this commercial game. The interview...
4
by: Jared | last post by:
Has anybody played the Mechwarrior Miniatures game? I'm trying to represent the game in python. First thing I need to do is make a playing field. Mechs will move on the playing field based on...
2
by: Bill Dandreta | last post by:
I would like to write a wordsearch puzzle game in Python that functions like this java program: http://javaboutique.webdeveloper.com/WordSearch/ I would like it to be cross platform (Linux,...
0
by: richard | last post by:
The Python Game Programming Challenge (otherwise known as PyWeek) is only a week away from starting! Theme voting has started! http://www.mechanicalcat.net/tech/PyWeek/1 Richard
7
by: Lucas Raab | last post by:
Saw this on Slashdot (http://developers.slashdot.org/article.pl?sid=05/09/17/182207&from=rss) and thought some people might be interested in it. Direct link to the article is...
4
by: Andreas R. | last post by:
OpenRTS is a new open source project, with the aim of creating a realtime strategy game. The game is developed in Python with Pygame. See http://www.openrts.org for more info about the game if you...
0
by: richard | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
1
by: Jerry Fleming | last post by:
Hi, I have wrote a game with python curses. The problem is that I want to confirm before quitting, while my implementation doesn't seem to work. Anyone can help me? #!/usr/bin/python # #...
0
by: =?windows-1252?Q?=3F=3F=3F=3F_=3F=3F=3F?= | last post by:
The fourth Python Game Programming Challenge (PyWeek) has now concluded with judges (PyWeek being peer-judged) declaring the winners: Individual: Which way is up? by Hectigo...
0
by: Richard Jones | last post by:
The fifth PyWeek is only a month away. Come along and join the fun: write a video game in a week! There's some really interesting new libraries that have popped up recently. Have a gander on the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.