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

如何将嵌套字典的所有值按组展示而非逐行输出(使用easygui)

问题:用easygui整齐展示嵌套字典的怪物属性

我是编程新手,正在完成练习评估,需使用easygui模块。我希望编写一个函数来展示完整的嵌套字典monster_cards,将每个子字典(怪物属性)以整齐的一组形式展示,而非当前代码中逐行弹出msgbox的形式。

原怪物字典结构:

#Dictionary for all the monster cards with a set of stats
monster_cards = {"Corpsemask":
                 {"Strength": 7,
                  "Speed": 1,
                  "Stealth": 25,
                  "Cunning": 15},
                 "Dustfigure":
                 {"Strength": 1,
                  "Speed": 6,
                  "Stealth": 21,
                  "Cunning": 19},
                 "Bonemonster":
                 {"Strength": 5,
                  "Speed": 15,
                  "Stealth": 18,
                  "Cunning": 22},
                 "Bonefoot":
                 {"Strength": 15,
                  "Speed": 20,
                  "Stealth": 23,
                  "Cunning": 6}}

当前函数代码:

def view_catalogue():
    if len(monster_cards) > 0:
        for card, card_stats in monster_cards.items():
            msg = easygui.msgbox("Monster: " + card)

            for key in card_stats:
                msg = easygui.msgbox(key + ": " + str(card_stats[key]))
            
                
    else:
        msg = easygui.msgbox("You have no monsters in your catalogue")

解决方案

给你两种修改方案,按需选择:

方案1:每个怪物单独弹出窗口展示完整属性

这种方式逐个弹出窗口,每个窗口展示一只怪物的所有属性,排版清晰:

import easygui

monster_cards = {"Corpsemask":
                 {"Strength": 7,
                  "Speed": 1,
                  "Stealth": 25,
                  "Cunning": 15},
                 "Dustfigure":
                 {"Strength": 1,
                  "Speed": 6,
                  "Stealth": 21,
                  "Cunning": 19},
                 "Bonemonster":
                 {"Strength": 5,
                  "Speed": 15,
                  "Stealth": 18,
                  "Cunning": 22},
                 "Bonefoot":
                 {"Strength": 15,
                  "Speed": 20,
                  "Stealth": 23,
                  "Cunning": 6}}

def view_catalogue():
    if len(monster_cards) > 0:
        for monster_name, stats in monster_cards.items():
            # 拼接当前怪物的所有信息,用换行符分隔
            monster_info = f"Monster: {monster_name}\n"
            for stat_name, value in stats.items():
                monster_info += f"{stat_name}: {value}\n"
            # 弹出窗口展示该怪物的完整属性,设置标题方便识别
            easygui.msgbox(monster_info, title=f"{monster_name} 属性详情")
    else:
        easygui.msgbox("你的怪物图鉴里没有任何怪物")

方案2:一次性展示所有怪物的完整图鉴

如果不想多次弹窗,也可以把所有怪物的信息拼接成一个大字符串,一次性弹出窗口展示全部内容:

import easygui

monster_cards = {"Corpsemask":
                 {"Strength": 7,
                  "Speed": 1,
                  "Stealth": 25,
                  "Cunning": 15},
                 "Dustfigure":
                 {"Strength": 1,
                  "Speed": 6,
                  "Stealth": 21,
                  "Cunning": 19},
                 "Bonemonster":
                 {"Strength": 5,
                  "Speed": 15,
                  "Stealth": 18,
                  "Cunning": 22},
                 "Bonefoot":
                 {"Strength": 15,
                  "Speed": 20,
                  "Stealth": 23,
                  "Cunning": 6}}

def view_catalogue():
    if len(monster_cards) > 0:
        # 初始化图鉴内容,添加分隔线让排版更整齐
        full_catalogue = "完整怪物图鉴\n" + "-"*20 + "\n"
        for monster_name, stats in monster_cards.items():
            full_catalogue += f"\nMonster: {monster_name}\n"
            # 给属性添加缩进,区分怪物名称和属性
            for stat_name, value in stats.items():
                full_catalogue += f"  {stat_name}: {value}\n"
        # 一次性弹出窗口展示所有内容
        easygui.msgbox(full_catalogue, title="怪物图鉴")
    else:
        easygui.msgbox("你的怪物图鉴里没有任何怪物")

改动说明

  1. 不再逐行弹窗,而是先把单只怪物(或所有怪物)的信息拼接成带换行的字符串,用\n实现换行,让内容排版整齐
  2. 使用f-string格式化字符串,比传统的+拼接更简洁易读,新手也容易理解
  3. 给弹窗设置标题,方便区分当前展示的内容

内容的提问来源于stack exchange,提问作者namethisuser

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 13:55:55