我用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()
uj5u.com熱心網友回復:
好像您縮進了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:
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429329.html
上一篇:用第一列的總和初始化一個變數
