import random
# Make a list of different animal or anything.
word_list = ["ardvark", "baboon", "camel"]
# Use the random module to randomly pick an animal in the list.
random_word = random.choice(word_list)
# print the choosen word.
print(random_word)
display = ["_" for _ in random_word]
lives = 6
# empty guess.
guess = ''
# empty guess list.
# while loop in display.
while "_" in display:
# ask the user to guess a letter from the chosen word.
guess = input("Guess a letter: ").lower()
# get the range and the len of the chosen word.
for i in range(len(random_word)):
# if the letter(guess) is equal to one of the letter in the chosen word.
if random_word[i] == guess:
display[i] = random_word[i]
if random_word != guess:
lives -= 1
print(lives)
# print the display with the right letter.
print(" ".join(display))
I was expecting that with every wrong answer, my life would go down by 1. But instead of every right or wrong answer, my life goes down by one. I don’t know what I’m doing wrong.
RESULTS:
baboon
Guess a letter: b
5
b _ b _ _ _
Guess a letter: r
4
b _ b _ _ _
Guess a letter:
>Solution :
It’s subtracting a life because your input of b is not equal to the word baboon, like you said in the line if random_word != guess:
My assumption is that you meant it to only subtract a life only if the guessed word/letter isn’t in the random_word. If this is correct, you can get the expected behavior by changing:
if random_word != guess:
lives -= 1
to:
if guess not in random_word:
lives -= 1
Which gives us this output:
camel
Guess a letter: c
c _ _ _ _
Guess a letter: b
5
c _ _ _ _
Guess a letter: a
c a _ _ _
Guess a letter: d
4
c a _ _ _
...