Dictionary int对象中的列表不是iterab

2024-04-25 16:32:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我和我的朋友开发一个奇怪的discord bot,其中一部分代码涉及到创建一个字典,它可以保存包含一个项目名称的列表,这是需要花费的。你知道吗

作为参考,一个名为“userShop”的dict包含如下内容:

{'DiscordName#0000': [23, 'test item']},其中不一致名称是不言自明的,int是项的成本,string是项的名称。你知道吗

为了“购买”项目,用户键入命令!buyitem <cost> <name>,这将启动以下代码片段:

@bot.command(pass_context=True)
async def buyitem(ctx, buyFrom: str, *, item: str):
    userName = str(ctx.message.author)
    for itemName, cost in userShop[buyFrom]:
        if itemName == item:
            #blah blah blah

返回错误

for itemName, cost in userShop[buyFrom]: TypeError: 'int' object is not iterable

我能做些什么来解决这个问题? 谢谢。你知道吗


Tags: 代码in名称forbotitemintblah
1条回答
网友
1楼 · 发布于 2024-04-25 16:32:40

如果userShop[buyFrom]是这样的列表[23, 'test item'],则不能使用in在两个变量中直接赋值。你知道吗

您应该使用:

itemName = userShop[buyFrom][0]
cost = userShop[buyFrom][1]
if itemName == item:
    #blah blah blah

相关问题 更多 >