最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How do I stop it from taking multiple lives instead of just one from a wrong guess? - Stack Overflow

programmeradmin4浏览0评论
import random

word_list = ["aardvark", "baboon", "camel"]

chosen_word = random.choice(word_list)
print(chosen_word)


placeholder = ""
word_length = len(chosen_word)
for position in range(word_length):
    placeholder += "_"
print(placeholder)

game_over = False
correct_letters = []
lives = 6

while not game_over:
    guess = input("Guess a letter: ").lower()

    display = ""

    for letter in chosen_word:
        if letter == guess:
            display += letter
            correct_letters.append(guess)
        elif letter in correct_letters:
            display += letter
        else:
            display += "_"


    print(display)
    print(lives)


    if "_" not in display:
        game_over = True
        print("You win.")
    elif lives == 0:
        game_over = True
        print("You lose.")
    else:
        for i in range(len(chosen_word)):
            if guess not in chosen_word[i]:
                lives -= 1

Long story short this /\ /\ takes a life for EACH wrong letter in the word. I know why it does that but not necessarily how to change it so it only takes one instead for a wrong guess.

Its asking me to do more details to post so I will say this is for the 100 days of coding class. I'm just getting back to it still day seven just part 4 now.

import random

word_list = ["aardvark", "baboon", "camel"]

chosen_word = random.choice(word_list)
print(chosen_word)


placeholder = ""
word_length = len(chosen_word)
for position in range(word_length):
    placeholder += "_"
print(placeholder)

game_over = False
correct_letters = []
lives = 6

while not game_over:
    guess = input("Guess a letter: ").lower()

    display = ""

    for letter in chosen_word:
        if letter == guess:
            display += letter
            correct_letters.append(guess)
        elif letter in correct_letters:
            display += letter
        else:
            display += "_"


    print(display)
    print(lives)


    if "_" not in display:
        game_over = True
        print("You win.")
    elif lives == 0:
        game_over = True
        print("You lose.")
    else:
        for i in range(len(chosen_word)):
            if guess not in chosen_word[i]:
                lives -= 1

Long story short this /\ /\ takes a life for EACH wrong letter in the word. I know why it does that but not necessarily how to change it so it only takes one instead for a wrong guess.

Its asking me to do more details to post so I will say this is for the 100 days of coding class. I'm just getting back to it still day seven just part 4 now.

Share Improve this question edited 2 hours ago Barmar 782k56 gold badges546 silver badges660 bronze badges asked 3 hours ago Logan SmithLogan Smith 92 bronze badges 3
  • 2 You could just break after subtracting from lives once to leave that loop. It looks like that will work fine. – Carcigenicate Commented 3 hours ago
  • Should the game end after the first mistake? – PM 77-1 Commented 3 hours ago
  • the final for loop could be replaced entirely with if guess not in chosen_word: lives -= 1 – JonSG Commented 3 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

In your else block, you are looping through every letter in chosen_word and reducing lives each time guess is not found in a letter. This results in multiple life deductions per wrong guess.

# remove the print(lives) and use when there is a wrong guess.

    if guess in chosen_word:
        for position in range(len(chosen_word)):
            if chosen_word[position] == guess:
               display = display[:position] + guess + display[position + 1:]
    else:
        lives -= 1  # if letter not found, only 1 life deduction will happen
        print("Remaining lives",lives)
发布评论

评论列表(0)

  1. 暂无评论