如何遍历输入变量的数组,并读取输出变量b

2024-10-03 09:13:55 发布

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

例如我有

commandz=[["yes","no"],["hi","hi"]]
async def handle_command(message):
    print('Noticed: ' + message.content)
    if message.content == 'tokenreset'+str(key):
        await client.send_message(message.channel, 'code accepted')
    i = 0
    for i in commandz[i][0]:
        comm = commandz[i][0]
        if comm == message.content:
            await client.send_message(message.channel, commandz[i][1])

我收到的错误信息是

C:\Users\trisimix>python "c:\Users\trisimix\compsocbot\main.py"
Found saved token in stored.py, use phrase tokenreset1424629785956179 to undo this.
Logged in as[198866998225141760]NOTAKOALAONACOMPUTERINVENEZUELA
--------
Noticed: hi
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\trisimix\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "c:\Users\trisimix\compsocbot\main.py", line 34, in on_message
    await handle_command(message)
  File "c:\Users\trisimix\compsocbot\main.py", line 42, in handle_command
    comm = commandz[i]
TypeError: tuple indices must be integers or slices, not str

我试图让我的程序使用if语句对照数组检查每个命令,然后用输出响应。你知道吗


Tags: inpyclientmessageifcontentawaithi
1条回答
网友
1楼 · 发布于 2024-10-03 09:13:55

从代码的第6行开始(即for循环):

for comm in commandz:
    if comm[0] == message.content:
        await client.send_message(message.channel, comm[1])

您的代码的问题是,您正在循环一个字符串的字符(在commandz[0][0]),而您真正想要的是循环嵌套列表中的列表(即["yes","no"]["hi","hi"],这只是在commandz)。你知道吗

请注意,我使用变量comm作为commandz中的列表,而不是这些列表中的第一个索引,即yeshi,我认为您打算将其用作

相关问题 更多 >