1
@bot.command(name='randnum')
async def randnum(ctx, arg1, arg2):
  randnum = random.randint(*arg1, *arg2)
  await ctx.send('spinning...')
  time.sleep(2)
  await ctx.send('your random number is', randnum)

arg1 and arg2 will be the numbers the user enters, then the bot picks a random one from between these values.

When i run it it gives me the TypeError: randnum() missing 1 required keyword-only argument: 'arg2' Why does this happen and how can i fix it?

golger
  • 21
  • 2

1 Answers1

1

The issue with your code is that the random.randint() method requires two integer arguments, but arg1 and arg2 are strings that need to be converted into integers before they can be used in the random.randint() method.

You can fix the error by changing the randnum line to the following:

randnum = random.randint(int(arg1), int(arg2))

This will convert the arg1 and arg2 strings into integers before passing them to random.randint().

Additionally, the await ctx.send('your random number is', randnum) line will raise a TypeError because you're passing two arguments to ctx.send() instead of one. You can fix this by concatenating the string and the random number using the + operator like this:

await ctx.send('your random number is ' + str(randnum))

So to sum up, your code should look like this:

@bot.command(name='randnum')
async def randnum(ctx, arg1, arg2):
  randnum = random.randint(int(arg1), int(arg2))
  await ctx.send('spinning...')
  time.sleep(2)
  await ctx.send('your random number is ' + str(randnum))
  • thanks! it worked i tried something similar to this using int() but i didnt do it right – golger Mar 28 '23 at 02:22
  • @golger it probably didn't work correctly because you put `*` infront of your variables, which in python is the [unpacking operator for lists](https://stackoverflow.com/questions/400739/what-does-asterisk-mean-in-python) – Anton Apr 02 '23 at 13:29