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

2021-05-13 15:19:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一些帮助。这里有我的两个清单:

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"]

好吧,很多人误解了我,所以我有两个列表,我用随机选择为了从每个列表中选择一个单词,一旦我们有了这些单词,我需要将它们打印出来,比如,如果选择了hot和weak,那么它将显示为“hot is to cold as weak is the uuuuuu?”我真的需要帮助,详细的步骤将不胜感激。在

3条回答
网友
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
网友
2楼 ·

{{cd2>可以使用函数{cd2}:

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?")
网友
3楼 ·

试试。。。在

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

相关问题