简体   繁体   English

我得到这个错误,我真的不知道它是什么意思

[英]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):为了确定该代码的确切部分导致错误,将其分成单独的行,像这样(除了使用实际的描述性变量名称而不是ab等):

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.

相关问题 我收到此错误,不知道该怎么办 - I get this error and don't know what to do with it 我不知道那个错误是什么意思以及如何解决这个问题。 我想将 Country 键和 France 值添加到字典中 - I don't know what that error means and how to solve this. I want to add Country key and France value to the dictionary 我不明白这个错误<function get_multiples_of_3 at 0x10a341510>或者这是什么意思?</function> - I don't understand this error <function get_multiples_of_3 at 0x10a341510> or what this means? 我什至不知道infile&gt; outfile是什么意思。 我应该如何使用它? - I don't even know what infile > outfile means. How am I supposed to use it? 我有这个错误,我不知道该怎么办 - I have this error and I don't know what to do 我在 api 中收到错误,可能是关于 pymysql.connect 的,但我不知道发生了什么 - I get the error in my api maybe about pymysql.connect but I don't know what happened 我不知道这是什么错误,它是 flask 和 url_for function - I don't know what error is this it is flask and with url_for function 我在安装SWITIFY package 时出错,有人知道这是什么意思吗? - I get an error when I install the SWITIFY package, does anyone know what it means? 我的函数中的打印语句出现语法错误。 我不知道我做错了什么 - I get syntax error on my print statement in my function. I don't know what I am doing wrong IndexError但我不知道怎么了 - IndexError but I don't know what is wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM