[英]I get this error and I really don't know what it means
so I have this code(discord.py):所以我有这段代码(discord.py):
@client.command()
async def buyrole(ctx, role: int):
print(serverdata[f'{ctx.guild}'][role].split(' '))
print(int(serverdata[f'{ctx.guild}'][role].split(' ')[1]))
for i in range(baldata[f'{ctx.guild}'].__len__()):
if(i):
if(f'{str(ctx.message.author).split("#")[0]}' in baldata[f'{ctx.guild}'][i]):
userbal = int(baldata[int(baldata[f'{ctx.guild}'][i].split('#')[1].split(' ')[1])])
if(userbal > int(serverdata[f'{ctx.guild}'][role].split(' ')[1])):
buy = discord.utils.get(ctx.guild.roles, name=serverdata[f'{ctx.guild}'][role].split(' ')[0])
if(not buy in ctx.message.author.roles):
add = int(baldata[f'{ctx.guild}'][i].split('#')[1].split(' ')[1]) - int(serverdata[f'{ctx.guild}'][role].split(' ')[1])
baldata[f'{ctx.guild}'][i] = f'{ctx.message.author} {add}'
await ctx.message.author.add_roles(buy)
await ctx.send(f'role {buy} purchased')
else:
await ctx.send('you already have that role')
else:
await ctx.send('you do not have enough money to buy that role, keep being active!')
and I get this error and I don't know what it means and how to fix it:我收到此错误,但我不知道这意味着什么以及如何解决:
Ignoring exception in command buyrole:
Traceback (most recent call last):
File "C:\Users\roeyd\PycharmProjects\roleShop\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/roeyd/PycharmProjects/roleShop/main.py", line 97, in buyrole
userbal = int(baldata[int(baldata[f'{ctx.guild}'][i].split('#')[1].split(' ')[1])])
KeyError: 20
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\roeyd\PycharmProjects\roleShop\venv\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\roeyd\PycharmProjects\roleShop\venv\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\roeyd\PycharmProjects\roleShop\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 20
thank you so so much for helping I really appreciate it I am new and having some trouble so helping is incredible非常感谢你的帮助我真的很感激我是新来的,遇到了一些麻烦所以帮助是不可思议的
A key error occurs whenever you query a dictionary for a key and the entry does not exist so whats happening is in line 97 whenever you query the users balance每当您查询字典中的键并且该条目不存在时,就会发生键错误,因此每当您查询用户余额时,都会在第 97 行发生什么
userbal = int(baldata[int(baldata[f'{ctx.guild}'][i].split('#')[1].split(' ')[1])])
(I can't be more specific cus that code is a pain to read.) you query a dictionary with the key 20 and the key does not exist so it raises an exception (我不能更具体,因为代码读起来很痛苦。)你用键 20 查询一个字典,但键不存在,所以它引发了一个异常
In order to determine which exact part of that code is causing the error, break it up into separate lines, like so (except use actual descriptive variable names rather than a
, b
, etc):为了确定该代码的确切部分导致错误,将其分成单独的行,像这样(除了使用实际的描述性变量名称而不是
a
, b
等):
a = baldata[f'{ctx.guild}']
b = a[i]
c = b.split('#')
d = c[1]
e = d.split(' ')
f = e[1]
g = int(f)
h = baldata[g]
userbal = int(h)
Having a whole bunch of code chained together like you did makes it really hard to read and debug.像您一样将一大堆代码链接在一起使得阅读和调试变得非常困难。 All of your code is filled with chained, dense statements like the erroring one, and thus it's going to be really difficult to understand exactly what's causing the error.
你所有的代码都充满了链式的、密集的语句,就像错误代码一样,因此很难准确理解是什么导致了错误。 If you replace that line with the above code, it'll be obvious where the actual error is happening.
如果用上面的代码替换该行,那么实际错误发生的位置就会很明显。
However I recommend going through all your code and replacing those long chains with more broken-up statements, so that your code is readable.但是,我建议检查所有代码并用更多破碎的语句替换那些长链,以便您的代码可读。 Currently it's next to impossible to understand.
目前几乎无法理解。 If you come back to it later it's going to take you a long time to figure out what it does.
如果您稍后再回过头来看它,您将需要很长时间才能弄清楚它的作用。 Code is meant to be executed by a computer, sure, but your main focus should be readability rather than density.
当然,代码是要由计算机执行的,但是您的主要关注点应该是可读性而不是密度。
After looking at your broken-up code, the two parts that seem like the culprits are the following lines:查看您的分解代码后,看起来像罪魁祸首的两部分是以下几行:
a = baldata[f'{ctx.guild}']
...
h = baldata[g]
...
So, if that's the case, I'd focus on why the value is being set to 20
, and/or why it's not in baldata
.因此,如果是这种情况,我将重点关注为什么将值设置为
20
和/或为什么它不在baldata
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.