0

How can I make this code work so that the user can input their guesses into the function until either they get the whole word correctly guessed or there are no more lives left? Right now, the user can only input one string, and then the loop abruptly ends.

secret_words_list = ['voldemort', 'hogwarts']  
def hangman():
    lives = 5
    while lives >= 0:
        answer = random.choice(secret_words_list)
        guess = raw_input('Write your answer here: ')
        hangman_display = ''
        for char in answer:
            if char in guess:
                hangman_display += char
                lives -= 1
            elif char == ' ':
                hangman_display += char
            else:
                hangman_display += "-"
                lives -= 1
        if hangman_display == answer:
            print("You win")
    print(hangman_display) 

2 Answers2

0
import random
secret_words_list = ['voldemort', 'hogwarts']  
def hangman():
    lives = 5
    while lives >= 0:
        answer = random.choice(secret_words_list)
        guess = raw_input('Write your answer here: ')
        hangman_display = ''
        for char in answer:
            if char in guess:
                hangman_display += char
                #lives -= 1
            elif char == ' ':
                hangman_display += char
            else:
                hangman_display += "-"
                #lives -= 1
        if hangman_display == answer:
            print("You win")
            break
        else:
            lives -=1
    print(hangman_display) 

hangman()

I din't understand your exact requirement but is this what you are looking for?

The program interaction was something like below,

Write your answer here: vol
-o------
Write your answer here: hog
hog-----
Write your answer here: hogwart
hogwart-
Write your answer here: hogwarts
You win
hogwarts
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
Vinay
  • 952
  • 2
  • 10
  • 27
0

The reason why it is ending abruptly is because it's checking on a character by character basis rather than checking the whole word then deciding if the guess was incorrect.

Here's my code for this solution, documented so you can understand:

basically, have a variable that acts as a switch, when you have a correct guess switch it, then have a check after the 'for' loop to see whether a life needs to be taken away or not.

You can see that this is what I'm doing with the 'correct' variable that I create before the loop, and check following.

Hope this helps ^.^ Connor

edit:

i'm going to break this down a little bit so that it's not a huge dump :P

Check the code if you can't make sense of this.

You receive input, test that it is one letter, and do the doohickey with the display.

checking over each character is...

#we need to check if we need to take a life away
correct = False

This is where the 'switch' I talked about is created, just a Boolean variable.

#loop through the word to guess, character by character.
for char in word:
    #if the character is in the old display, add it to the new on.
    if char in lastdisplay:
        display += char

Here, if the character was displayed beforehand we will bring it up on the new display.

    #if the character is in the guess, add it to the display.
    elif char in guess:   
        display += char
        #we made a correct guess!
        correct = True

If the guessed character is the character that we are currently checking add it to the display, and flip the switch to 'True'

    #otherwise we need to add a blank space in our display.
    else:               
        if char == ' ':
            display += ' '  #space
        else:
            display += '_'  #empty character

Otherwise, nothing has happened, add in a space/blank and continue on with the loop.

#if we didn't get a correct letter, take a life.
if not correct:
    lives -= 1

Here's were we check the 'switch', if it is 'True', we don't need to take a life,

Otherwise the 'switch is 'False' and we take a life.

Community
  • 1
  • 1
Connor Turlan
  • 45
  • 1
  • 6