jeudi 23 mai 2019

How to use random.choice() with functions? [duplicate]

This question already has an answer here:

I have 18 multiple choice questions listed as functions in one file, and I want to call 10 of those questions randomly from a main file. If I use random.choice() with a list of integers or strings it works fine, but if I pass it a list of functions it runs all of the functions in order, ignoring the range I set. I've read a lot of documentation, help files and similar cases on forums, but I'm still scratching my head over it. At this point I think random.choice just won't do what I need it to. Does anyone see my mistake or know an alternative?

I've tried random.choice(), .choices(), .shuffle() I've tried passing the functions in a list and a dictionary, entering the list name or the entire list of options directly into the .choice(), printing(random.choice(list)) but I get the same result every time.

import random

def Q1():
    print('Question 1')
def Q2():
    print('Question 2')
def Q3():
    print('Question 3')    

list = [Q1(),Q2(),Q3()]
for i in range(5):
    random.choice(list)
    i+=i




Aucun commentaire:

Enregistrer un commentaire