List within Dictionary - int object is not iterable

I'm working on a strange discord bot for my friends and I, and one part of the code involves creating a dictionary, which can hold lists containing the name of an item, and it's cost.

For reference, a dict named "userShop" holds stuff like so:

{'DiscordName#0000': [23, 'test item']}, where the discord name is self-explanatory, the int is the cost of the item, and the string is the name of the item.

In order to "purchase" the item, the user types in the command !buyitem <cost> <name>, which starts the following snippit of code:

@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

Which returns the error

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

What can I do to fix this? Thanks.

1 answer

  • answered 2018-04-14 15:55 penguin2048

    If userShop[buyFrom] is a list like this [23, 'test item'] you can not directly assign the two values in two variables using in.

    You should use:

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