Skip to content
Advertisement

How to iterate over a range and then append items from list to it?

Right now the for loop that adds reactions at the end of the message takes number_of_responses, which is what I want. But I would also like the e.add_field fields be added based on number_of_responses (along with the right emoji in the name field and then add items from the list answers to their values).

async def run_script(params):
    params = params
    channel = client.get_channel(<obfuscated>)
    emoji_numbers = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣"]
    question = params[0] # string
    answers = ast.literal_eval(params[1]) # list
    number_of_responses = len(answers) # int
    e = discord.Embed(title='Title', description='Desc.', color=discord.Color.blue())
    e.add_field(name=question, value='u200b', inline=False)
    e.add_field(name='1️⃣', value='Mobiel', inline=False)
    e.add_field(name='2️⃣', value='Tablet', inline=False)
    e.add_field(name='3️⃣', value='Laptop', inline=False)
    e.add_field(name='4️⃣', value='Desktop', inline=False)
    e.add_field(name='5️⃣', value='TV', inline=False)
    message = await channel.send(embed=e)
    for i in range(number_of_responses):
        await message.add_reaction(emoji_numbers[i])
    for i in range(4, -1, -1):
        time.sleep(1)
    await channel.send("Going to sleep now")
    await client.close()

Advertisement

Answer

No need to loop over indices. Just zip the answers and the emoji_numbers:

for emoji, answer in zip(emoji_numbers, answers):
    e.add_field(name=emoji, value=answer, inline=False)
    await message.add_reaction(emoji)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement