## hangman

### hangman

for some reason my game will add a body part to the hangman when i guess a letter correct, and do nothing when i guess a letter wrong. My code is below:

Code: Select all
`import randomhangmanpics = ['''    +---+    |   |        |        |        |        |  =========''', '''    +---+    |   |    0   |        |        |        |  =========''', '''    +---+    |   |    0   |    |   |        |        |  =========''', '''    +---+    |   |    0   |   /|   |        |        |  =========''', '''    +---+    |   |    0   |   /|\  |        |        |  =========''', '''    +---+    |   |    0   |   /|\  |   /    |        |  =========''', '''    +---+    |   |    0   |   /|\  |   / \  |        |  =========''']words = 'kayak baloon ant spider tree typewriter game hangman canada yellow purple horse turnip computer telephone scissors' .split()def getrandomword(wordlist):    #this function returns a random string from the passed list of strings    wordindex = random.randint(0, len(wordlist) - 1)    return wordlist[wordindex]def displayboard(hangmanpics, missedletters, correctletters, secretword):    print(hangmanpics[len(missedletters)])    print()    print('Missed Letters:', end=' ')    for letter in missedletters:        print(letter, end=' ')    print()    blanks = '_' * len(secretword)    for i in range(len(secretword)): #replaces blanks with correctly guessed letters        if secretword[i] in correctletters:            blanks = blanks[:i] + secretword[i] + blanks[i+1:]    for letter in blanks: # Show the secret word with spaces in between each letter        print(letter, end=' ')    print()def getguess(alreadyguessed):    #returns the letter the player entered. this function makes sure the player entered a single letter, and not something else.    while True:        print('Guess a letter.')        guess = input()        guess = guess.lower()        if len(guess) != 1:            print('Please enter a single letter.')        elif guess in alreadyguessed:            print('You have already guessed that letter. Choose again.')        elif guess not in 'abcdefghijklmnopqrstuvwxyz':            print('please enter a LETTER.')        else:            return guessdef playagain():    #this function returns TRUE if the player wants to play agin, otherwise it returns false.    print('Do you want to play agin? (yes or no)')    return input().lower().startswith('y')print('H A N G M A N')missedletters = ''correctletters = ''secretword = getrandomword(words)gameisdone = Falsewhile True:    displayboard(hangmanpics, missedletters, correctletters, secretword)    guess = getguess(missedletters + correctletters)    if guess in secretword:        correctletters = correctletters + guess        #check if the player has won        foundallletters = True        for i in range(len(secretword)):            if secretword[i] not in correctletters:                foundallletters = False                break        if foundallletters:            print('Yes! the secret word is "' + secretword + '"! You have won!')            gameisdone = True        else:            missedletters = missedletters + guess            #check if the player has guessed too many times and lost            if len(missedletters) == len(hangmanpics) - 1:                displayboard(hangmanpics, missedletters, correctletters, secretword)                print('You have run out of guesses!\nAfter ' + str(len(missedletters)) + ' missed guesses and ' + str(len(correctletters)) + ' correct guesses.')                gameisdone = True                    #ask the player if they want to play again        if gameisdone:            if playagain():                missedletters = ''                correctletters = ''                gameisdone = False                secretword = getrandomword(words)            else:                break                `
jack

Posts: 5
Joined: Thu Jun 13, 2013 10:29 pm

### Re: hangman

Your else clause (where you handle the case of a wrong guess) is indented once too many times.
hansn

Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

### Re: hangman

thank you, it works now!
jack

Posts: 5
Joined: Thu Jun 13, 2013 10:29 pm

Return to Game Development

### Who is online

Users browsing this forum: No registered users and 4 guests