首页 > 解决方案 > IndexError:列表索引超出范围 discord.py

问题描述

好的,所以我尝试制作一个 lvl 系统并且它到目前为止工作,但现在我想制作一个命令,它在嵌入中显示 lvl 和 xp。

但我得到一个错误IndexError: list index out of range

我这样定义我的xp和lvl

async def lvl_up(self, user):
    cur_xp = user['xp']
    cur_lvl = user['lvl']

并想像这样将其发送到嵌入中

embed=discord.Embed()
embed.add_field(name="Level", value=user[0]['lvl'], inline=True)
embed.add_field(name="XP", value=user[0]['xp'], inline=True)
await ctx.send(embed=embed)

但这些价值观不像我认为的那样工作......

标签: pythondiscord.py

解决方案


通常,索引超出范围错误仅表示您要求的索引不存在。所以这意味着: value=user[0]['lvl']可能是问题,因为它找不到用户的索引 0。


推荐阅读