减少 1 生命值每次猜错。

huangapple go评论68阅读模式
英文:

Reduce lives by 1 every wrong guess

问题

It seems like you want assistance with your Python code. To clarify, you want to decrement your "lives" variable only when the user makes a wrong guess, but it's currently decrementing with every input. You can modify your code like this:

import random

word_list = ["ardvark", "baboon", "camel"]
random_word = random.choice(word_list)

print(random_word)

display = ["_" for _ in random_word]

lives = 6
guess = ''

while "_" in display and lives > 0:
    guess = input("Guess a letter: ").lower()
    found = False  # Add a flag to track if the guess is correct

    for i in range(len(random_word)):
        if random_word[i] == guess:
            display[i] = random_word[i]
            found = True  # Set the flag to True for correct guesses

    if not found:
        lives -= 1

    print(" ".join(display))

if "_" not in display:
    print("Congratulations! You guessed the word.")
else:
    print("Out of lives. The word was:", random_word)

This modified code should decrement "lives" only when the user makes a wrong guess, not with every input.

英文:
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:

答案1

得分: 1

这是减少一条生命,因为您输入的 b 不等于单词 baboon,就像您在这一行中所说的 if random_word != guess:

我的假设是,您的意思是只有在猜测的单词/字母不在 random_word 中时才减少一生。如果这是正确的,您可以通过更改以下内容来获得预期的行为:

if guess not in random_word:
        lives -= 1

这样我们就得到了这个输出:

骆驼
猜一个字母:c
c _ _ _ _
猜一个字母:b
5
c _ _ _ _
猜一个字母:a
c a _ _ _
猜一个字母:d
4
c a _ _ _
...
英文:

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 _ _ _
...

huangapple
  • 本文由 发表于 2023年5月14日 09:28:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定