Python 我应该如何实现random.sample,以便它在不重复的情况下从3个变量中选择2个?

Python 我应该如何实现random.sample,以便它在不重复的情况下从3个变量中选择2个?,python,python-3.x,Python,Python 3.x,我正在做一个使用类和OOP的小测验。我想让代码从3个问题中随机选择2个,但我不知道是否应该使用random.sample,或者在哪里使用它 import random class Question: def __init__(self, prompt, answer): self.prompt = prompt self.answer = answer question_prompts = ['What is the capital of Englan

我正在做一个使用类和OOP的小测验。我想让代码从3个问题中随机选择2个,但我不知道是否应该使用random.sample,或者在哪里使用它

import random

class Question:
    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

question_prompts = ['What is the capital of England? (a) London (b) Liverpool (c) Glasgow.  Answer(Type a, b or c): ', 'What is the capital of France? (a) Callais (b) Paris (c) Bologne. Answer(Type a, b or c): ', 'What is the capital of Netherlands? (a) Amsterdam  (b) Tilburg (c) Eindhoven. Answer(Type a, b or c): ',]


questions[Question(question_prompts[0], 'a'), Question(question_prompts[1], 'b'), Question(question_prompts[2], 'a'),]


def run(questions):
    score = 0
    answer = (questions)
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
            print('correct!')
我想让代码在3个问题中随机选择2个,而不重复。

使用该函数

i、 e

您可以使用:

>>将numpy作为np导入
>>>设置选项的集合=[1,2,3,4,5]
>>>np.random.choice(a=一组选项,大小=2,替换=False)
数组([2,3])
>>>np.random.choice(a=一组选项,大小=2,替换=False)
数组([5,1])
>>>np.random.choice(a=一组选项,大小=2,替换=False)
数组([1,2])

call
run(random.sample(问题2)
for question in random.sample(questions, 2):