在Python中改组和获取类对象

时间:2016-03-16 16:58:39

标签: python arrays random shuffle

我有一个名为allTheQuestions的课程:

answers = ['def', 'break', 'len(string)']
    q1options = ['func', 'def', 'function', 'define'];
    q2options = ['break', 'loop out', 'stop', 'abandon'];
    q3options = ['string.length', 'str(length)', 'len(string)', 'len.string'];
    q1 = raw_input('What is the keyword for functions in Python? ');
    q2 = raw_input('What keyword terminates a loop in Python? ');
    q3 = raw_input('How do you get the length of a string? ');
    questionList = [q1, q2, q3]
    questionShuffled = random.shuffle(questionList)

我有一个名为问题的函数:

def questions():
    score = 0
    print(allTheQuestions.questionShuffled)
    q1answer = allTheQuestions.q1options[1];
    print(q1answer)

我希望该功能可以随机播放我的问题列表,然后以随机顺序打印出来。到目前为止,我可以注释掉这一行print(allTheQuestions.questionShuffled),它将按照他们在课堂上的顺序运行问题。我怎么能这样做,以便它会以不同的顺序随机化它们?

感谢

1 个答案:

答案 0 :(得分:0)

random.shuffle对列表本身进行操作并返回None。你需要类似的东西 =INDEX($A$1:$P$1,SMALL(IF(Sheet2!$D2:$P2<0,COLUMN($D2:$P2)),2)) 然后说random.shuffle(questionsList)