-
Notifications
You must be signed in to change notification settings - Fork 0
Home
gratefulfrog edited this page Apr 12, 2015
·
4 revisions
Welcome to the Hangman_github wiki!
- Global variables are evil, pass arguments to functions instead of using globals. If you want to use globals for parametrization, put them at the top of the file and consider them as CONSTANTS that are Never Modifed by the Code. For example, hard coded path names, if you want to use such things...
- Be careful with multiple levels of embedded 'if/else'. As a general rule, never do it, as an additional rule if a function takes more than one screen then it is too long. A function should do just one thing, but it can get help from friends!
- Never call functions directly from the file, encapsulate all calls in functions. Look at the code below and see how it makes things a lot better:
# put these constants at the top of the file, for user updating,
# note that they are only here for convenience, since they are
# referenced only once at the top level call!
WORDLIST_FILENAME = "./words.txt" # a relative path is used whenever possible
MIN_GUESSES = 8 # you always get at least this many guesses
# then define this top level call to play the game
def play(wordFile = WORDLIST_FILENAME, minGuesses = MIN_GUESSES):
"""top level call:
loads the word file
then plays rounds of the game until the player wants to stop.
"""
wordList = loadWords(wordFile)
while(hangman(chooseWord(wordList),
minGuesses,
[])):
pass
# and modify the hangman loop exit to return 'True' if the player
# wants another round or 'False' to quit.
# For example, use this code and update the loop
def playAgainB(): # guessesLeft):
'''
asks the player to play again
returns: True if he wants to play again,
returns: False otherwise
'''
return raw_input('Would you like to play again? (y or n): ').lower() == 'y'