Python中移除导入字典内列表括号(f-string打印场景)
解决选择题选项格式问题
你可以把正确答案和错误答案合并成一个列表,再通过字符串拼接去掉列表自带的括号与引号,修改后的代码如下:
# 合并正确答案与错误答案为完整选项列表 all_options = [current_question.question_answer] + current_question.incorrect_answer # 将列表转为逗号分隔的字符串 options_str = ", ".join(all_options) user_answer = input(f"{current_question.category}, {current_question.difficulty} {current_question.question_text} Choices: {options_str} ").lower()
修改后输出的选项会变成:
Choices: Multiplayer Online Battle Arena (MOBA), Real Time Strategy (RTS), First Person Shooter (FPS), Role Playing Game (RPG)
如果想让正确答案位置随机(符合常规选择题的出题逻辑),可以额外添加打乱列表的代码:
import random all_options = [current_question.question_answer] + current_question.incorrect_answer random.shuffle(all_options) # 打乱选项顺序 options_str = ", ".join(all_options)
内容的提问来源于stack exchange,提问作者Adam Knight
相关产品推荐
相关产品推荐

