无法向Python中的函数传递多个参数

2024-09-29 19:25:26 发布

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

它不允许我向函数传递多个参数。Python一直认为我还在定义第一个参数。守则:

async def find(ctx, user, parachannel):
    user = int(user)
    parachannel = int(parachannel)
    funchannel = bot.get_channel(parachannel)
    fun_guild = bot.get_guild(880108797820026881)
    memberuser = fun_guild.get_member(user)
    fun_calc = 0
    async for message in funchannel.history(limit=10):
        if message.author == memberuser:
            fun_calc = fun_calc + 1
    return fun_calc


gen_calc = find(user, 880123663318409277)
print(gen_calc)

获取此错误:
discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:find()缺少1个必需的位置参数:“parachannel”

完全回溯:

2021-09-16T11:40:52.403142+00:00 app[worker.1]: Ignoring exception in command analyze:
2021-09-16T11:40:52.404102+00:00 app[worker.1]: Traceback (most recent call last):
2021-09-16T11:40:52.404120+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core .py", line 85, in wrapped
2021-09-16T11:40:52.404121+00:00 app[worker.1]:     ret = await coro(*args, **kwargs)
2021-09-16T11:40:52.404122+00:00 app[worker.1]:   File "main.py", line 132, in analyze
2021-09-16T11:40:52.404123+00:00 app[worker.1]:     gen_calc = find(user, 880123663318409277)
2021-09-16T11:40:52.404142+00:00 app[worker.1]: TypeError: find() missing 1 required positional argument: 'parachannel'
2021-09-16T11:40:52.404151+00:00 app[worker.1]: 
2021-09-16T11:40:52.404152+00:00 app[worker.1]: The above exception was the direct cause of the following exception:
2021-09-16T11:40:52.404152+00:00 app[worker.1]: 
2021-09-16T11:40:52.404154+00:00 app[worker.1]: Traceback (most recent call last):
2021-09-16T11:40:52.404170+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 939, in invoke
2021-09-16T11:40:52.404170+00:00 app[worker.1]:     await ctx.command.invoke(ctx)
2021-09-16T11:40:52.404172+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 863, in invoke
2021-09-16T11:40:52.404173+00:00 app[worker.1]:     await injected(*ctx.args, **ctx.kwargs)
2021-09-16T11:40:52.404181+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
2021-09-16T11:40:52.404181+00:00 app[worker.1]:     raise CommandInvokeError(exc) from exc
2021-09-16T11:40:52.404195+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: find() missing 1 required positional argument: 'parachannel'

Tags: inpyapplineexceptioncalcfindext
3条回答

如果没有必要将ctx作为第一个参数,您可以这样做

async def find(user, parachannel, ctx = None):
    # You codes
    return fun_calc

现在,您可以在不使用ctx参数的情况下调用函数

gen_calc = find(user, 880123663318409277)

函数需要三个参数,但只传递两个参数。所以,你自然会看到一个错误。您应该为参数提供一些默认值。例如,类似这样的事情:

async def find(ctx = None, user = "Mushtaq", parachannel = 0):
    user = int(user)
    parachannel = int(parachannel)
... and the rest of your code.

find(ctx, user, parachannel)函数需要3个参数:ctxuserparachannel。但是当你调用这个函数时,你只给出了两个参数ctx = useruser = 880123663318409277。您缺少第三个参数parachannel

相关问题 更多 >

    热门问题