Home > Mobile >  How can i remove the brackets from a list inside a dictionary that is imported from another file tha
How can i remove the brackets from a list inside a dictionary that is imported from another file tha

Time:09-27

user_answer = input(f"{current_question.category}, {current_question.difficulty}\n{current_question.question_text}\nChoices: {current_question.question_answer}, {current_question.incorrect_answer}\n").lower()

This fstring prints:

Entertainment: Video Games, easy
League of Legends, DOTA 2, Smite and Heroes of the Storm are all part of which game genre?
Choices: Multiplayer Online Battle Arena (MOBA), ['Real Time Strategy (RTS)', 'First Person Shooter (FPS)', 'Role Playing Game (RPG)']

I'm trying to get the fstring to print the correct answer with the incorrect answers as a part of a multiple choice quiz, but the brackets are from the incorrect answers being inside a list.

CodePudding user response:

Change current_question.incorrect_answer to ', '.join(current_question.incorrect_answer) to convert it to a comma-delimited string.

CodePudding user response:

user_answer = input(f"{current_question.category}, {current_question.difficulty}\n{current_question.question_text}\nChoices: {current_question.question_answer}, {' '.join(current_question.incorrect_answer)}\n").lower()

I assume you just want to switch the lust into a string?

CodePudding user response:

Assuming you want to randomize the answers each time (otherwise the quiz might be a bit too easy?) you can try something like this:

import random

category = "Entertainment: Video Games"
difficulty = "easy"
question_text = "League of Legends, DOTA 2, Smite and Heroes of the Storm are all part of which game genre?"
question_answer = "Multiplayer Online Battle Arena (MOBA)"
incorrect_answer = ['Real Time Strategy (RTS)', 'First Person Shooter (FPS)', 'Role Playing Game (RPG)']

answers = incorrect_answer.copy()   [question_answer]
random.shuffle(answers)
answers_str = "\n".join(answers)
prompt = f"{category}, {difficulty}\n{question_text}\nChoices: {answers_str}\n"

user_answer = input(prompt).lower()

Output:

Entertainment: Video Games, easy
League of Legends, DOTA 2, Smite and Heroes of the Storm are all part of which game genre?
Choices: First Person Shooter (FPS)
Real Time Strategy (RTS)
Multiplayer Online Battle Arena (MOBA)
Role Playing Game (RPG)
  • Related