简体   繁体   中英

How can i remove the brackets from a list inside a dictionary that is imported from another file that is printed inside an fstring in Python?

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.

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

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?

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:\n {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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM