【问题标题】:Randomly select a pair of elements from a two dimensional array and place them in a sentence in python从二维数组中随机选择一对元素,并在python中将它们放在一个句子中
【发布时间】:2020-10-03 14:52:36
【问题描述】:
import random

quizwords = [["hot", "cold"],["summer","winter"],["hard","soft"],["dry","wet"],["simple","complex"],["light","darkness"],["weak","strong"],["male","female"],["sad","happy"],["win","lose"],["small","big"],["ignore","pay attention"],["sell","buy"],["succeed","fail"],["reject","accept"],["prevent","allow"],["exclude","include"]]

c = 0
random.shuffle(quizwords)
for i in quizwords:
    print(i[0][0],"is to",i[0][1],"as",i[1][0],"is to...")
    ans = input()

到目前为止,这是我的代码,我相信它会输出'e is to c as s is to...' 我试图让它改为说'排除是包含,因为拒绝是......(答案:接受)'

【问题讨论】:

    标签: python arrays random


    【解决方案1】:
    import random
    
    quizwords = [["hot", "cold"],["summer","winter"],["hard","soft"],["dry","wet"],["simple","complex"],["light","darkness"],["weak","strong"],["male","female"],["sad","happy"],["win","lose"],["small","big"],["ignore","pay attention"],["sell","buy"],["succeed","fail"],["reject","accept"],["prevent","allow"],["exclude","include"]]
    
    c = 0
    random.shuffle(quizwords)
    for i in range(len(quizwords) - 1)[::2]:
        first_pair = quizwords[i]
        second_pair = quizwords[i+1]
        print(f'{first_pair[0]} is to {first_pair[1]} as {second_pair[0]} is to...')
        ans = input()
    

    由于您已经在 for 循环中遍历列表中的项目,因此执行两级索引就是提取每个单词的第一个字母。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      相关资源
      最近更新 更多