Python 如何在for循环中分类非类型错误?
尝试将非类型从该for循环中拉出时遇到问题,这样我就不会因为此错误而使我的bot崩溃:Python 如何在for循环中分类非类型错误?,python,sqlite,discord,Python,Sqlite,Discord,尝试将非类型从该for循环中拉出时遇到问题,这样我就不会因为此错误而使我的bot崩溃: Ignoring exception in command rank: Traceback (most recent call last): File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped ret = await coro(*a
Ignoring exception in command rank:
Traceback (most recent call last):
File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "D:\Life\discordbot\main.py", line 232, in rank
await ranking_menu.start(ctx)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\menus\__init__.py", line 967, in start
await super().start(ctx, channel=channel, wait=wait)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\menus\__init__.py", line 707, in start
self.message = msg = await self.send_initial_message(ctx, channel)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\menus\__init__.py", line 962, in send_initial_message
kwargs = await self._get_kwargs_from_page(page)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\menus\__init__.py", line 939, in _get_kwargs_from_page
value = await discord.utils.maybe_coroutine(self._source.format_page, self, page)
File "E:\Programing\discordbot\lib\site-packages\discord\utils.py", line 343, in maybe_coroutine
return await value
File "D:\Life\discordbot\main.py", line 207, in format_page
table = ("\n".join(f'{idx + 1}. {self.ctx.bot.get_user(entry[0]).name} (XP: {entry[1]} | Level: {entry[2]} \n'
File "D:\Life\discordbot\main.py", line 207, in <genexpr>
table = ("\n".join(f'{idx + 1}. {self.ctx.bot.get_user(entry[0]).name} (XP: {entry[1]} | Level: {entry[2]} \n'
AttributeError: 'NoneType' object has no attribute 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "E:\Programing\discordbot\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: AttributeError: 'NoneType' object has no attribute 'name'
还有其他的方法,但我认为生成器是一种很好的编写方法,而不是试图在一行中完成所有工作。它还过滤条目/用户,并跳过用户ID与现有用户不对应的条目:
async def format_page(self, menu, entries):
def get_table_rows():
for index, (user_id, xp, level) in enumerate(entries, start=1):
user = self.ctx.bot.get_user(user_id)
if user is None:
# If no user with the current 'user_id' exists, skip it.
continue
yield f"{index}. {user.name} (XP: {xp} | Level: {level})\n"
fields = []
table = "\n".join(get_table_rows())
fields.append(("Ranks", table))
return await self.write_page(menu, fields)
您在哪里调用
格式页面
?entries
是什么样子的?entries是从数据库查找返回的一行,其中包含用户id、xp、级别和格式页面,在这里它被称为类的一部分`````````````````````````菜单=MenuPages(source=LocalXpMenu(ctx,xp\u ranking)),wait ranking\u menu.start(ctx)``所以,条目
是元组列表还是类似的东西?然后entry
表示当前元组,entry[0]
表示用户id
,对吗?您能否断言条目[0]
是一个整数,而不是字符串之类的东西Client.get\u user
需要一个整数。entry[0]
是一个正确的整数。那么,如果对所有条目都是这样,那么get\u user
可能返回None
的唯一原因是,您传入的ID中至少有一个与任何现有用户不对应——换句话说,不存在具有该ID的用户。谢谢,伙计!那么发电机究竟是如何工作的呢?我想我记得听说它每次运行都会计数?我在这里可能100%错了/认为生成器就像一个函数,但有状态。生成器一次“生成”一个项目到调用代码,直到生成器耗尽并且没有更多的项目可以生成为止。每次生成一个项时,生成器的执行都会暂停,直到请求下一个项为止。“\n”.join(get\u table\u rows())
使用生成器中的所有项。噢!我在上面看了一个视频,但它直到现在才点击,这很有趣,非常感谢你的帮助,伙计!
async def format_page(self, menu, entries):
def get_table_rows():
for index, (user_id, xp, level) in enumerate(entries, start=1):
user = self.ctx.bot.get_user(user_id)
if user is None:
# If no user with the current 'user_id' exists, skip it.
continue
yield f"{index}. {user.name} (XP: {xp} | Level: {level})\n"
fields = []
table = "\n".join(get_table_rows())
fields.append(("Ranks", table))
return await self.write_page(menu, fields)