提问人:Feeko 提问时间:10/27/2023 更新时间:10/27/2023 访问量:55
如何在不破坏此代码的情况下将条件变回 False?
How can I turn condition back to False without breaking this code?
问:
我正在开发这款刽子手游戏,这部分是为了跟踪玩家的生活。它从列表中随机提取一个单词,然后获取该单词并将其转换为空格 (_)。如果你猜对了,它会用字母填充空白,当你猜到所有正确的字母时,你就赢了。如果你猜错了,它应该加载一个慢慢显示在绞索上的简笔画,就像老式的刽子手游戏一样。
如果你猜错了,你会得到代码上方显示的列表中的第一项,称为“阶段”。当您猜错 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.
当我在 for 循环下将变量“found_letter”设置为 False 并将其设置为 True 时,即使您猜对了,它也会显示“阶段”数字。因此,在设置为 False 后立即猜测会使上述代码中的“生命”无论如何都会丢失 1。
我所期望的是,当你猜对时,它不会给你任何“阶段”数字,当你猜对后猜错时,它会检测到它有效,但它不会,因为变量“found_letter”永远不会恢复为 False。
我试图移动底部代码的缩进,if 检查 found_letter 是否等于 False。我甚至尝试更改条件以使其类似于 for 循环,并利用 while 循环的一部分来检查多个条件,但是每当我将“found_letter”设置为 False 时,它就会通过从“阶段”生成数字并将“生命”减少 1 来破坏游戏。
我没能弄清楚其中的逻辑。我正在努力在编程中更好地处理逻辑。你们能提供一些技巧或窍门吗?我知道这个地方是用来找出编码问题的解决方案,但我需要帮助,因为这是我最挣扎的地方。谢谢。
答:
在测试代码时,很明显,每次猜测后都需要将“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教程。
评论
found_letter = False
found_letter = False
False
for
while
for letter in range(len(chosen_word)):