Home > Back-end >  How can i random.choice a question without the answer for a discord robot quiz game?
How can i random.choice a question without the answer for a discord robot quiz game?

Time:12-25

I just started learning how to code ( in python ) and i was wondering how can I randomly ask questions for a quiz without the answer that follows?

For example, I'd want the robot to ask 'What's the capital of France?' but without it saying 'Paris'?

questions = [("What's the capital of France?", "Paris"), ("Who painted the Mona Lisa?", "Da Vinci")]

Ty :)

CodePudding user response:

random.choice will just return a tuple (since those are the items in your list). So you can access just the first element while printing by doing [0].

For example, print(random.choice(questions)[0]).

In the larger program you'd want to assign the tuple to a variable, so that later you fetch the answer for the same question (by using [1]) instead of randomly selecting again.

  • Related