Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

python - ValueError, not enough values to unpack using Discord API

I'm trying to make a event so that whenever someone send a message, the bot will respond with the message with ever other character swapped case. This is what I have so far:

@client.event
async def on_message(message):

   username = message.author.name

   if message.author == client.user:
       return

   exp = []
   for index, element in message.content:
       if index % 2 == 0:
           exp.append(element.swapcase())
       else:
           exp.append(element)

       response = "".join(exp)
   await message.channel.send(response)

When I run this i get a error message in Python saying:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:UsersProgrammerPycharmProjectsDiscordBottingvenvlibsite-packagesdiscordclient.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:UsersProgrammerPycharmProjectsDiscordBottingot.py", line 33, in on_message
    for index, element in x: # message.content
ValueError: not enough values to unpack (expected 2, got 1)

EDIT: So I used a different method of iterating over the string shown below:

@client.event
async def on_message(message):

    username = message.author.name

    if message.author == client.user:
        return

    exp = []

    for i in range(len(message.content)):
        if i % 2 == 0:
            exp.append(message.content[i].swapcase())
        else:
            exp.append(message.content[i])

        response = "".join(exp)
    await message.channel.send(response)

However I still don't know why the first method didn't work.

question from:https://stackoverflow.com/questions/66064239/valueerror-not-enough-values-to-unpack-using-discord-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can iterate through a string, that's no a problem. Just

for element in message.content:
    print(element)

will do a job.

But, if you want an index you must wrap that with a enumerate, so you need

for index, element in enumerate(message.content):
    print(index, element)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...