Python - 随机选择要在测验中显示的单词

时间:2015-01-08 02:39:18

标签: python list random

我需要一些帮助。这里有两个列表:

wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
             "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]

wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
         "pay attention", "sell", "fail", "accept", "allow", "include"]

好吧,很多人误解了我,所以我有这两个列表,我使用random.choice从每个列表中选择一个单词,一旦我们有了这些单词,我需要将它们打印出来作为这样的问题因为,如果选择了热和弱,那么它将被显示为,"热是冷,因为弱到___?"我真的需要这方面的帮助,我们将非常感谢详细的步骤。

3 个答案:

答案 0 :(得分:1)

使用random库进行随机选择,并使用zip确保每个元素都与之相关联:

import random

words = zip(wordlist1, wordlist2)
print random.choice(words)

for word1, word2 in words:
    print word1, "is the opposite of", word2

答案 1 :(得分:0)

您可以使用random包并使用random.choice功能:

import random

wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
              "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]

wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
              "pay attention", "sell", "fail", "accept", "allow", "include"]

word1 = random.choice(wordlists1)
word2 = random.choice(wordlists2)
print("Are "+word1+" and "+word2+" opposites?")

答案 2 :(得分:0)

尝试......

import random      
print(" question " + random.choice(wordlists1) + " question " + random.choice(wordlists2))