我想製作一個「終端黑客」遊戲,但我被困在某個地方。
這是我的代碼當前如下:生成一個列表的隨機排列
import random
candidateWords = ['AETHER', 'BADGED', 'BALDER', 'BANDED', 'BANTER', 'BARBER'] #etc, etc
def wordlist():
for index, item in enumerate(random.sample(list(candidateWords), 8)):
print(index, ") ", item, sep='')
one = random.choice(candidateWords)
print(one)
print("Welcome to the Guess-The-Word Game.\nThe Password is one of these words:")
我試圖做的8個字的清單,並列舉它給每一個字一個數字。然後,從8個單詞中,我需要隨機選擇一個單詞作爲答案,但我不知道如何。
我想使用random.sample()
和random.choice()
。
注意:我忘了從wordlist()中取出(單詞)。請忽略:)和「one = random.choice(candidateWords)print(one)」部分完全是我想做我想做的事的猜測。它不起作用 – John