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. ''' # FILL IN YOUR CODE HERE... import string allLetters = string.ascii_lowercase available = '' for char in allLetters: if not char in lettersGuessed: available += char return available 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. ''' # FILL IN YOUR CODE HERE... guess = '' for l in secretWord: if l not in lettersGuessed: guess += '_' else: guess += l return guess 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 ''' # FILL IN YOUR CODE HERE... guessed = True for l in secretWord: if not l in lettersGuessed: guessed = False break return guessed 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. ''' # FILL IN YOUR CODE HERE... import string print 'Welcome to the game, Hangman!' print 'I am thinking of a word that is '+str(len(secretWord))+' letters long.' guessLeft = 8 mistakeMade ='' lettersGuessed = list() while guessLeft > 0: print '-------------' print 'You have '+str(guessLeft)+' guesses left.' print 'Available letters: '+getAvailableLetters(lettersGuessed) guess = raw_input( 'Please guess a letter: ') guessInLowerCase = guess.lower() if guessInLowerCase in lettersGuessed: print "Oops! You've already guessed that letter:"+getGuessedWord( secretWord, lettersGuessed ) else: lettersGuessed.append( guessInLowerCase ) if guessInLowerCase in secretWord: print 'Good guess: '+getGuessedWord( secretWord, lettersGuessed ) if isWordGuessed(secretWord, lettersGuessed): print '-------------' print 'Congratulations, you won!' break else: print "Oops! That letter is not in my word: "+getGuessedWord( secretWord, lettersGuessed ) guessLeft -= 1 if guessLeft == 0: print '-------------' print "Sorry, you ran out of guesses. The word was else." hangman( 'tact' )
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question