dimanche 18 octobre 2020

Can I print only one of these questions at a time?

I need to randomise some questions and get one question at a time. I have about 200 questions in my list and I want my python code to randomly pick one out of the 200. I then want it to remove all questions that have already been printed from the list so that they cannot show up again. My current code randomises the order in which these questions come out. Is there a way to make it so the first question in the output is printed and then the second is printed and then the third one at a time? This is how my code looks (except with 200 questions):

import random

questions = [
    'define math',
    'define french',
    'define english',
    'define spanish',
    'define czech',
    'define slovak',
    'define whatever',
]

while questions:

    index = random.randrange(0, len(questions))

    selected = questions[index]
    questions.pop(index)
    
    print(selected)

And my output(which will look different every time it’s run because its random) looks like this

define czech
define math
define whatever
define french
define english
define slovak
define spanish

Can I make it so ‘define czech’ is printed on its own and then when I run the code again, ‘define math’ comes up and then ‘define whatever’ comes up in that randomised order? Any help will be appreciated!




Aucun commentaire:

Enregistrer un commentaire