![](/img/trans.png)
[英]i'm doing a code but i don't see what i'm doing wrong. i keep getting this error 'ValueError: invalid literal for int() with base 10: '2a'
[英]I keep on getting this error, can't find what I'm doing wrong
这是该错误的代码行:
if msg.startswith("!del"):
encouragements = []
if "encouragements" in db.keys():
index = int(msg.split("!del",1)[1])
delete_encouragement(index)
encouragements = db["encouragements"]
await message.channel.send(encouragements)
这是我不断收到的错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 69, in on_message
index = int(msg.split("!del", 1)[1])
IndexError: list index out of range
每次尝试运行 .del 命令时都会出现错误。
当 msg 被拆分但结果只包含一个元素时,可能会发生异常“IndexError:list index out of range”。 该代码试图获取第二个元素,但只存在一个元素。 请参阅以下示例:
msg="!del"
print('one',msg.split("!del",1))
msg="something"
print('two',msg.split("!del",1))
msg="something"
print('three', msg.split("!del",1)[1])
Output:
one ['', '']
two ['something']
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-13-4193103020d6> in <module>
6
7 msg="something"
----> 8 print('three',msg.split("!del",1)[1])
9
10
IndexError: list index out of range
这是一个基本的 python 错误。 请查看有关split()和序列索引的文档。
如果您为您的机器人使用discord.Client
object,您可能需要查看commands.Bot
object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.