【问题标题】:argument of type 'int' is not iterable assistance needed'int' 类型的参数不是需要可迭代的帮助
【发布时间】:2021-08-15 11:44:10
【问题描述】:

嗨,我正在尝试找出这个错误argument of type 'int' is not iterable 我基本上是在制作一个包含 10 个结果的编号列表。对于列表中的第一个结果,我想在数字 1. 之后添加一个皇冠

这是我的工作:

async def leaderboard(self, ctx):
    """Displays the top 10 members in the server"""

        async with self.bot.pool.acquire() as conn:
            res = await conn.fetch("SELECT user_id,lvl,xp,total_xp FROM levels WHERE guild_id = $1 ORDER BY GREATEST(total_xp) DESC LIMIT 10", ctx.guild.id)
            #0: user_id, 1: lvl
            mess = "Here is a list of the current top ten active members."
            x = 1
            crown = f":crown: {x}" if 1 in x else f"{x}" #type int error happens here
            for ele in res:
                user = self.bot.get_user(int(ele[0]))
                mess +="**{}.** {} on **Level {: <3}** with **{: <4} XP**\n\n".format(crown,user.name,ele[1], ele[2])
                x+=1
            embed = discord.Embed(title="Leaderboard", description=f"{mess}")
            await ctx.send(embed=embed)

错误似乎发生在crown = f":crown: {x}" if 1 in x else f"{x}"这一行

帮助表示赞赏。

【问题讨论】:

  • x 是一个数字,你对1 in x 有什么期望?
  • 如果列表中的数字是 1,那么将 :crown: 放在该数字前面就是我要在这里实现的目标。
  • 什么列表? x 是一个单一的数字,1,它是您在出错的那一行之前分配的。
  • 上面的代码创建了一个编号从 1 到 10 的结果列表我想在列表中识别数字 1 并在该数字前面添加一个皇冠。
  • 也许确实如此,但 x 绝对不是那个列表。你试过调试吗?

标签: python python-3.x discord.py


【解决方案1】:

if 1 in x 等同于 if 1 in 1 - xint不是列表,但在 cmets 中,您以某种方式一直坚持认为它是一个列表 (?)。

认为你想要完成的是在列表中的第一个条目(排行榜上的“第一”)中添加一个皇冠,在这种情况下你 想检查“1”是否在列表中,但想检查for-loop 中的当前条目是否是第一个。在res 中永远不会有1,因为restuples 的列表,而不是ints,它们也不包含索引。

您可以通过使用enumerate() 同时遍历索引和元素来获取列表中元素的当前index,然后使用该索引查看它们在排行榜中的排名。为了很好地打印它,它们的位置是index + 1,因为索引是从 0 开始的。这样您也可以完全删除 x 变量,因为它无论如何都不会做任何事情。

res = await conn.fetch("SELECT user_id,lvl,xp,total_xp FROM levels WHERE guild_id = $1 ORDER BY GREATEST(total_xp) DESC LIMIT 10", ctx.guild.id)
#0: user_id, 1: lvl
mess = "Here is a list of the current top ten active members."

# Iterate over the list of results along with the index in the list (i)
for i, ele in enumerate(res):
    user = self.bot.get_user(int(ele[0]))
    # Res is ordered descending by your query, so the first element is the #1
    if i == 0:
        # Add a crown in front of the string if this is #1 (= element 0)
        mess += ":crown: "
    # Add the rest of the string, if a crown is necessary then it was prepended above ^
    mess +="**{}.** {} on **Level {: <3}** with **{: <4} XP**\n\n".format(i + 1,user.name,ele[1], ele[2])

您的问题非常令人困惑,乍一看,cmets 并没有真正的帮助。 x 不是一个列表,它是一个int,而1 在您的res 列表中不是

【讨论】:

  • 这只会返回列表中第一个带有皇冠的条目?列表中的其他 9 个条目丢失。
  • 不,它返回所有这些,最后一行对列表中的每个条目执行,因此每个条目都添加到 mess 字符串中。它 not 在 if 语句中,这可能是您在尝试解决此问题时搞砸的地方?如果你把最后一行 inside if 那么它只会添加第一行。
  • 感谢您抽出宝贵时间回答我的问题。您的答案在理论上确实有效,但并不完全是我想要的。您的代码似乎分别从数据库返回条目,而不是将结果附加到一个完整列表中。这是结果的截图ibb.co/YpJ1bBx
  • 我的答案中没有ctx.send,所以这不是是因为我。如果它发送多个嵌入,那么看起来您添加了 for 循环的 ctx.send inside?你应该把它放在 below 循环中,所以它只发送 one 嵌入 every name。所以我的代码 is 正确并且 does 可以满足您的需求。它将所有内容附加到for-loop 中的一个完整列表中,并且 for 循环之后,您将整个内容放在一个嵌入中,您应该发送一次 .列表逐个增长的事实证实了您将 send 放入循环中。
猜你喜欢
  • 2023-02-20
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
相关资源
最近更新 更多