简体   繁体   中英

how to save a variable in a if statement in a loop

I'm trying to program hangman and I run into a problem. In line 48 you see that I'm trying to copy the word that has been guessed so far. But the problem is when the program asks for the next letter. For example, if the user have guessed the letter 'h', it says h____ and the next turn I guess the letter e it says _e___ but I want it to say in this example he___ .

word = 'hello'
guessed = False
guess_word = secret_word(word)
a = '____'

while guessed == False:
    letter = ask_letter_from_user()

    if is_the_letter_in_the_word(word, letter):
        locatie_letter_in_woord = location_of_letter_in_word(word, letter)
        a = replace(guess_word, locatie_letter_in_woord, letter)
        print(a)

Seems like a and guess_word should actually be one variable.

You are passing guess_word to the replace function after every guessed letter, but you are never updating its value. Instead, you're updating a .

Get rid of a . Next time, give all your variables meaningful names, then you might realized that you have two variables for one purpose:-)

Instead of calling replace(guess_word, locatie_letter_in_woord, letter) , simply do a = a[:locatie_letter_in_woord] + letter + a[locatie_letter_in_woord+1:] . This will prevent the previous letter from being overwritten.

Output:

wich letter do you want to try?: h
well done! you guessed the letter
youre guessed letters are: h
a: h___
wich letter do you want to try?: e
well done! you guessed the letter
youre guessed letters are: h,e
a: he__

Try this:

you can turn the word into a list and have each letter checked one by one

word = 'hello'
guessed = False
found = []
guess_word = secret_word(word)

while guessed == False:
    guess= ask_letter_from_user()

    if is_the_letter_in_the_word(word, letter):
        print('well done! you guessed the letter')
        word_as_list = list(guess_word)
        indices = [i for i, letter in enumerate(word) if letter == guess]
        for index in indices:
            word_as_list[index] = letter
            found.append(letter)
        print('youre guessed letters are: ' + list_joining(found))

        guess_word = "".join(word_as_list)
        print(guess_word)
    

In line 47 in place of a = replace(guess_word, locatie_letter_in_woord, letter) write a = replace(a, locatie_letter_in_woord, letter) .

wich letter do you want to try?: h
well done! you guessed the letter
youre guessed letters are: h
h___
wich letter do you want to try?: e
well done! you guessed the letter
youre guessed letters are: h,e
he__
wich letter do you want to try?: 

Still, this program has a problem. When you are calling the function:

def location_of_letter_in_word(word, letter):
    return word.find(letter)

Look carefully for the second l in hello it will return the location of the first l .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM