Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Created October 23, 2012 01:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooreniemi/3936074 to your computer and use it in GitHub Desktop.
Save mooreniemi/3936074 to your computer and use it in GitHub Desktop.
600x
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 computer's 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.
'''
# FILL IN YOUR CODE HERE...
intro = str(len(secretWord))
lettersGuessed = []
guess = str
mistakesMade = 0
print 'Welcome to the game, Hangman!'
print ('I am thinking of a word that is ') + intro + (' letters long.')
print ('------------')
while mistakesMade < 8:
if isWordGuessed(secretWord,lettersGuessed):
return ('Congratulations, you won!')
print ('You have ') + str(8-mistakesMade) + (' guesses left.')
print ('Available letters:') + getAvailableLetters(lettersGuessed)
guess = raw_input('Please guess a letter:').lower()
if guess in secretWord:
if guess in lettersGuessed:
print ('Oops! Youve already guessed that letter:') + getGuessedWord(secretWord, lettersGuessed)
print ('------------')
else:
lettersGuessed.append(guess)
print ('Good guess:') + getGuessedWord(secretWord, lettersGuessed)
print ('------------')
else:
if guess in lettersGuessed:
print ('Oops! Youve already guessed that letter:') + getGuessedWord(secretWord, lettersGuessed)
print ('------------')
else:
lettersGuessed.append(guess)
mistakesMade += 1
print ('Oops! That letter is not in my word:') + getGuessedWord(secretWord, lettersGuessed)
print ('------------')
return ('Sorry, you ran out of guesses. The word was ') + secretWord
@Theofilos-Chamalis
Copy link

Great work pal! Thanks for your effort and time to write this and help people in EdX 600x excercises. I'd like to point out though that you need 2 minor corrections in lines 41 and 49. It has to be 'Oops! You've already guessed that letter:' on both respectively since they do stupid error checks in all prints of the code. Good job and keep it up ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment