TypeError: must be str, not int Python Discord bot Array problems creating an embed

Unable to find out what is wrong with the final line of this code (there is more below, just trying to work out why it’s going wrong! If anyone could help that would be greatly appreciated.

Attempting to create multiple fields in a discord embed. I feel like it’s a simple solution, I have no idea where I’ve tripped up though.

code is completely functional up to await ctx.channel.send(answers) and answers is a functional array.

    try:
        idCell = modsheet.find(str(idTag), in_column=1)
        
        #pull the entire row and put it as a string
        answers = modsheet.row_values(idCell.row)
        await ctx.channel.send(answers)

        answers.remove(answers[0])
        for y in answers:
            modAppEmbed.add_field(name="Q" + str(y) + " Answer: ", value=str(answers[y]))
        
        modchatchannel = bot.get_channel(814629441967357992)
        await modchatchannel.send(embed=modAppEmbed)

2021-03-08T22:48:29.646688+00:00 app[worker.1]: Ignoring exception in command modapp: 2021-03-08T22:48:29.647867+00:00 app[worker.1]: Traceback (most recent call last): 2021-03-08T22:48:29.647910+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped 2021-03-08T22:48:29.647910+00:00 app[worker.1]: ret = await coro(*args, **kwargs) 2021-03-08T22:48:29.647910+00:00 app[worker.1]: File "bot.py", line 277, in modapp 2021-03-08T22:48:29.647911+00:00 app[worker.1]: modAppEmbed.add_field(name="Q" + str(y) + " Answer: ", value=answers[y]) 2021-03-08T22:48:29.647953+00:00 app[worker.1]: TypeError: list indices must be integers or slices, not str 2021-03-08T22:48:29.647954+00:00 app[worker.1]: 2021-03-08T22:48:29.647955+00:00 app[worker.1]: The above exception was the direct cause of the following exception: 2021-03-08T22:48:29.647955+00:00 app[worker.1]: 2021-03-08T22:48:29.647957+00:00 app[worker.1]: Traceback (most recent call last): 2021-03-08T22:48:29.648001+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 935, in invoke 2021-03-08T22:48:29.648001+00:00 app[worker.1]: await ctx.command.invoke(ctx) 2021-03-08T22:48:29.648001+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 863, in invoke 2021-03-08T22:48:29.648001+00:00 app[worker.1]: await injected(*ctx.args, **ctx.kwargs) 2021-03-08T22:48:29.648004+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped 2021-03-08T22:48:29.648004+00:00 app[worker.1]: raise CommandInvokeError(exc) from exc 2021-03-08T22:48:29.648040+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: list indices must be integers or slices, not str

Answer

Assuming answers is an array of str, the problem resides in that for loop:

for y in answers:
    modAppEmbed.add_field(name="Q" + str(y) + " Answer: ", value=str(answers[y]))

The y iterator would point to the element itself in this case, so you won’t be able to use it as an index for the answer array. Rather use y itself, by adjusting the loop to:

for y in answers:
    modAppEmbed.add_field(name="Q" + str(y) + " Answer: ", value=str(y))

Leave a Reply

Your email address will not be published. Required fields are marked *