如何在不破坏此代码的情况下将条件转回 False?

问题描述 投票:0回答:1

我正在开发这个 Hangman 游戏,这部分是为了跟踪玩家的生活。它从列表中随机抽取一个单词,然后将其转换为空格 (_)。如果你猜对了,它就会用字母填充空白,当你猜对所有正确的字母时,你就赢了。如果你猜错了,它应该加载一个在绞索上缓慢显示的简笔画,就像老式的刽子手游戏一样。

如果您猜错了,您将获得代码上方显示的列表中的第一项,称为“阶段”。当您猜错 6 次时,您会收到一条消息:“您输了”。问题就在这里...

当我输入错误时,它可以正常工作,甚至输入错误6次后也可以正常工作。变量“lives”下降-1,直到变为0。当它达到0时,游戏结束。当我猜对时,它会起作用,但如果我尝试猜错,它永远不会检测到它是错误的。

原因是因为变量'found_letter'默认设置为False,当你得到正确的结果时,它会将其设置为True。并且它不会回到 False。尝试再次切换到 False 会导致其他问题。

这是代码:

stages = [''' 
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']


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


import random
chosen_word = random.choice(word_list)

#To-do 1: - Create a variable called 'lives' to keep track of the number of lives left. Set 'lives' equal to 6
lives = 6
found_letter = False
#Testing code
print(f'Pssst, the solution is {chosen_word}.')

#Create blanks
display = []
for letter in chosen_word:
    letter = "_"
    display += letter


while ("_") in display and lives > 0:
    guess = input("Guess a letter: ")
    guess = guess.lower()
    print(("_") in display and lives > 0)

    for letter in range(len(chosen_word)):
        if guess == chosen_word[letter]:
            display[letter] = guess
            found_letter = True
    print(display)


#To-do 2: - If guess is not a letter in the 'chosen'word', then reduce 'lives' by 1. If 'lives' goes down to 0 then the
#game should stop and it should print "You lose."
    # for letter in range(len(chosen_word)):

    if found_letter == False:
        lives -= 1
        print(stages[lives])



    if lives == 0:
        print("You lose.")

print("You have won!")

#To-do 3: - print the ASCII art from 'stages' that corresponds to the current number of 'lives' the user has remaining.


当我在将变量“found_letter”设置为 True 的 for 循环下将其设置为 False 时,即使您猜对了,它也会显示“阶段”数字。因此,在设置为 False 后正确猜测会使上面代码中的“生命”损失 1。

我期望的是,当你猜对时,它不会给你任何“阶段”数字,当你猜对后猜错时,它会检测到它有效,但它不会,因为变量“found_letter”永远不会恢复为假。

我尝试移动底部代码的缩进,其中 if 检查found_letter 是否等于 False。我什至尝试更改条件以使其类似于 for 循环,并使用 while 循环的一部分来检查多个条件,但每当我将 'found_letter' 设置为 False 时,它就会通过从 'stages' 生成数字来破坏游戏,并且将“生命”减少 1。

我无法弄清楚其中的逻辑。我正在努力提高编程中的逻辑工作能力。你们还可以提供提示或技巧吗?我知道这个地方是为了找出编码问题的解决方案,但我需要帮助,因为这是我最困难的地方。谢谢你。

python for-loop while-loop boolean logic
1个回答
0
投票

在测试代码时,很明显,每次猜测后都需要将“found_letter”布尔变量重置为“False”值,才能产生所需的功能。以下是您的代码的重构版本。

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

import random
chosen_word = random.choice(word_list)

#To-do 1: - Create a variable called 'lives' to keep track of the number of lives left. Set 'lives' equal to 6
lives = 6

#Testing code
print(f'Pssst, the solution is {chosen_word}.')

#Create blanks
display = []
for letter in chosen_word:
    letter = "_"
    display += letter

while ("_") in display and lives > 0:
    found_letter = False                # Set this to a default of False here before every guess
    guess = input("Guess a letter: ")
    guess = guess.lower()
    #print(("_") in display)            # Removed this as it was always printing True if even one letter position was "_"

    for letter in range(len(chosen_word)):
        if guess == chosen_word[letter]:
            display[letter] = guess
            found_letter = True         # Now is the time to denote that a letter was guessed correctly
    print(display)

# To-do 2: - If guess is not a letter in the 'chosen'word', then reduce 'lives' by 1. 
# If 'lives' goes down to 0 then the game should stop and it should print "You lose."
# for letter in range(len(chosen_word)):

    if found_letter == False:
        lives -= 1
        print(stages[lives])

#If this point is reached determine if the game was won or lost

if lives == 0:
    print("You lose.")
else:
    print("You have won!")

仅供参考,我省略了暂存人物集,以专注于重构的部分。以下是一些主要修订。

  • 在“while”循环中的每次测试期间,“found_letter”变量不断重置为 False 值。
  • 仅当猜测与秘密单词中的字母之一匹配时,“found_letter”变量才会设置为 True 值。
  • 由于当至少有一个位置带有“_”字符时,游戏会继续在“while”循环中进行测试,因此测试该字符的打印语句的条件给出了一个具有误导性的虚假 True 答案,因此它被停用.
  • 一旦所有生命都耗尽或猜出了单词,就会检查该单词是否被正确猜测或玩家耗尽生命,并打印适当的“赢/输”措辞。

对其进行测试运行,以下是终端输出的一些最终行。

Guess a letter: e
['_', '_', 'd', '_', '_', '_', '_']

  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========

Guess a letter: v
['_', '_', 'd', 'v', '_', '_', '_']
Guess a letter: t
['_', '_', 'd', 'v', '_', '_', '_']
 
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========

You lose.

仅供参考,这个游戏的词是“ardvark”。

这只是推动游戏前进的多种方法之一。查看它,看看它是否满足您的需求。与往常一样,请继续参考有关使用条件测试和布尔变量的更多 Python 教程。

© www.soinside.com 2019 - 2024. All rights reserved.