无法将多个参数传递给 Python 中的函数
Can't pass multiple arguments to function in 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'
find(ctx, user, parachannel)
函数需要 3 个参数:ctx
、user
和 parachannel
。但是当你调用这个函数时,你只给出了两个参数。 ctx = user
和 user = 880123663318409277
。您缺少第三个参数 parachannel
.
你的函数需要三个参数,但你只传递了两个给它。因此,您自然会看到一个错误。你应该给你的参数一些默认值。例如这样的事情:
async def find(ctx = None, user = "Mushtaq", parachannel = 0):
user = int(user)
parachannel = int(parachannel)
... and the rest of your code.
如果不需要将 ctx
作为第一个参数,你可以
async def find(user, parachannel, ctx = None):
# You codes
return fun_calc
现在您可以在没有 ctx
参数的情况下调用您的函数。
gen_calc = find(user, 880123663318409277)
它不允许我将多个参数传递给我的函数。 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'
find(ctx, user, parachannel)
函数需要 3 个参数:ctx
、user
和 parachannel
。但是当你调用这个函数时,你只给出了两个参数。 ctx = user
和 user = 880123663318409277
。您缺少第三个参数 parachannel
.
你的函数需要三个参数,但你只传递了两个给它。因此,您自然会看到一个错误。你应该给你的参数一些默认值。例如这样的事情:
async def find(ctx = None, user = "Mushtaq", parachannel = 0):
user = int(user)
parachannel = int(parachannel)
... and the rest of your code.
如果不需要将 ctx
作为第一个参数,你可以
async def find(user, parachannel, ctx = None):
# You codes
return fun_calc
现在您可以在没有 ctx
参数的情况下调用您的函数。
gen_calc = find(user, 880123663318409277)