Home > Software design >  Can't pass multiple arguments to function in Python
Can't pass multiple arguments to function in Python

Time:09-17

It doesn't let me pass multiple arguments to my function. Python keeps thinking I'm still defining the first argument. The code:

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)

Getting this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: find() missing 1 required positional argument: 'parachannel'

Full Traceback:

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'

CodePudding user response:

find(ctx, user, parachannel) function requires 3 arguments: ctx, user and parachannel. But when you call the function you're only giving two arguments. ctx = user and user = 880123663318409277. You're missing the 3rd argument parachannel.

CodePudding user response:

Your function requires three arguments but you only pass two to it. So, naturally you see an error. You should give your parameters some default values. For example something like this:

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

CodePudding user response:

If it's not necessary to keep ctx as first argument, you can do

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

Now you can call your function without ctx argument.

gen_calc = find(user, 880123663318409277)
  • Related