对于循环打印所有步骤,但我只需要最后一个
For loop printing all steps, but i only need the last
我正在 python 制作刽子手游戏。
现在我陷入了一个问题。
我希望 for 循环打印显示
想要的输出:['_', '_', '_', '_', '_']
but it prints:
['_']
['_', '_']
['_', '_', '_']
['_', '_', '_', '_']
['_', '_', '_', '_', '_']
我知道这是 for 循环,但是如何让它只打印我想要的内容?
感谢您的宝贵时间和帮助!
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
# Testing code
print(f'Pssst, the solution is {chosen_word}.')
display = []
for letter in chosen_word:
display.append("_")
print(display)
guess = input("Guess a letter: ").lower()
您似乎缩进了 print(display)
语句并将其添加到 for 循环中。只需取消循环缩进,代码就可以工作了。
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
#Testing code
print(f'Pssst, the solution is {chosen_word}.')
display = []
for letter in chosen_word:
display.append("_")
print(display)
guess = input("Guess a letter: ").lower()
输出:
Pssst, the solution is aardvark.
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter:
我实施这个来练习。当然可以更详细,但作为参考应该有效:
import random
WORD_LIST = ["aardvark", "baboon", "camel"]
MAX_POINTS = 5
chosen_word = random.choice(WORD_LIST)
word = ["".join("_") for i in chosen_word]
print(word)
def guessing():
# Initial setup
initial_points = MAX_POINTS
print(f"initial_points: {initial_points}")
# store the guess history
history = set()
while True:
if "_" not in word:
print('you WIN!!')
break
elif initial_points < 1:
print('you LOSE!!')
break
guess = input("Guess a letter: ").lower()
# decrement initial points
if guess not in chosen_word \
and guess not in history \
and guess not in word:
initial_points -= 1
for i, c in enumerate(chosen_word):
if guess == c:
word[i] = guess
# Not decrement twice
history.add(guess)
# display
print(f"Points: {initial_points} - {word}")
print(f"Guessed letters: {history}")
guessing()
我正在 python 制作刽子手游戏。 现在我陷入了一个问题。 我希望 for 循环打印显示
想要的输出:['_', '_', '_', '_', '_']
but it prints:
['_']
['_', '_']
['_', '_', '_']
['_', '_', '_', '_']
['_', '_', '_', '_', '_']
我知道这是 for 循环,但是如何让它只打印我想要的内容? 感谢您的宝贵时间和帮助!
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
# Testing code
print(f'Pssst, the solution is {chosen_word}.')
display = []
for letter in chosen_word:
display.append("_")
print(display)
guess = input("Guess a letter: ").lower()
您似乎缩进了 print(display)
语句并将其添加到 for 循环中。只需取消循环缩进,代码就可以工作了。
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
#Testing code
print(f'Pssst, the solution is {chosen_word}.')
display = []
for letter in chosen_word:
display.append("_")
print(display)
guess = input("Guess a letter: ").lower()
输出:
Pssst, the solution is aardvark.
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter:
我实施这个来练习。当然可以更详细,但作为参考应该有效:
import random
WORD_LIST = ["aardvark", "baboon", "camel"]
MAX_POINTS = 5
chosen_word = random.choice(WORD_LIST)
word = ["".join("_") for i in chosen_word]
print(word)
def guessing():
# Initial setup
initial_points = MAX_POINTS
print(f"initial_points: {initial_points}")
# store the guess history
history = set()
while True:
if "_" not in word:
print('you WIN!!')
break
elif initial_points < 1:
print('you LOSE!!')
break
guess = input("Guess a letter: ").lower()
# decrement initial points
if guess not in chosen_word \
and guess not in history \
and guess not in word:
initial_points -= 1
for i, c in enumerate(chosen_word):
if guess == c:
word[i] = guess
# Not decrement twice
history.add(guess)
# display
print(f"Points: {initial_points} - {word}")
print(f"Guessed letters: {history}")
guessing()