Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 32 Online
OpenStudy (anonymous):

problem set 3 hangman.. I wish it's almost done, but when I got correct word it should print new one which is replace the word with '_' but it's adding up. can anybody help me?

OpenStudy (anonymous):

def hangman(): ansWord = choose_word(wordlist) #ansWord = list(ansWord) letters = 'abcdefghijklmnopqrstuvwxyz' guessNumber = 8 guess = '' while (guessNumber > 0): guessLetter = raw_input('Please guess a letter: ') letters = letters.replace(guessLetter, "") def goodGuess(guessLetter, guess): guess = guess for c in ansWord: if c in guessLetter: guess = guess + c else: guess = guess + '-' return guess def badGuess(guessLetter, guess): return guess if guessLetter in ansWord: print 'Good guess: '+ goodGuess(guessLetter, guess) guess += goodGuess(guessLetter, guess) else: guessNumber -= 1 print 'Opps! That letter is not in my word: ', badGuess(guessLetter, guess) guess = badGuess(guessLetter, guess) print '-----------' print 'You have' ,guessNumber, 'guesses left.' print 'Available letters: ', letters if goodGuess(guessLetter, guess) == ansWord: print 'congratulations, you won!' elif guessNumber == 0 : print 'You lost! The answer is' , ansWo

OpenStudy (rsmith6559):

Line 5 in the function has a single double quote, which starts a literal string, so everything until the next double quote is treated as a string instead of code. Define your functions outside of your hangman function and then call them where needed. It will help make your code more readable. One thing to remember in this assignment is that strings are arrays of characters and you can address each letter individually.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!