You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:45:42