Benny
Benny

Reputation: 83

Discord.py Calculator: Multiple Values?

My current code is limited to only taking in two values. without going through the entire alphabet, is there a way for me to take in more than two values and use them? Thank you.

   @client.command() 
async def add(ctx,a:int,b:int): 
    await ctx.send(f"{a} + {b} = {a+b}") #Adds A and B

@client.command() 
async def sub(ctx,a:int,b:int): 
    await ctx.send(f"{a} - {b} = {a-b}") #Subtracts A and B

@client.command() 
async def multiply(ctx,a:int,b:int): 
    await ctx.send(f"{a} * {b} = {a*b}") #Multplies A and B

@client.command() 
async def divide(ctx,a:int,b:int): 
    await ctx.send(f"{a} / {b} = {a/b}") #Divides A and B
    

Upvotes: 2

Views: 5581

Answers (5)

XINSDEATH
XINSDEATH

Reputation: 1

@client.command() async def multiply(ctx, num1: int, num2: int): a = num1*num2 await ctx.send(f"the answer is {a}")

@client.command() async def add(ctx, num1: int, num2: int): a = num1+num2 await ctx.send(f"the answer is {a}")

@client.command() async def subtract(ctx, num1: int, num2: int): a = num1-num2 await ctx.send(f"the answer is {a}")

@client.command() async def devide(ctx, num1: int, num2: int): a = num1/num2 await ctx.send(f"the answer is {a}")

try using this instead

Upvotes: 0

Yeti
Yeti

Reputation: 165

As suggested by the other answers, passing *arg as an argument would consume all the rest of the arguments passed and provide you with a tuple.

Since using eval openly is dangerous, I recommend parsing your expression manually.

If you want a more easier solution, you can use an external module to do this for you such as SimpleEval.

Here's a simple implementation of an 'add' command using *argument:

def add_numbers(numbers: tuple):
    if operator == "+":

        # escape all empty strings if any
        numbers_escaped = list(filter(None, numbers))

        # numbers is a tuple of str
        # We should make them all ints first, so we can perform addition
        numbers = list(map(int, numbers_escaped))

        # Now, we can return the addition result
        result = sum(numbers)
        
        return result


@bot.command()
async def add(ctx, operator: str,  *numbers):
    output = add_numbers(numbers)
    await ctx.send(f"Your result is: {output}")
    

Upvotes: 0

THEMEGACODERS
THEMEGACODERS

Reputation: 11

My way is simple, but for power you need to do ** instead of ^ Also if your code is like

bot = commands.Bot(command_prefix = "prefix")

You should do

@bot.command()
async def math(ctx, *, expression:str):
    calculation = eval(expression)
    await ctx.send('Math: {}\nAnswer: {}'.format(expression, calculation))

But if it is like

client = commands.Bot(command_prefix = "prefix")

^ It says client instead of bot you should change the @bot.command() into a @client.command(), but I'm pretty sure a lot of people know that so just copy the code.

Upvotes: 0

vazzza
vazzza

Reputation: 57

There is no need to separate the operations and the numbers just need to identify the maths question as a string and eval do the heavy lifting. Sorry for the late answer.

@client.command()
async def calc(ctx, operation:str):
  await ctx.send(eval(operation))

This code only works assuming your client is defined. Heres an example:

client = commands.Bot(command_prefix = "!!")

Upvotes: 2

Nurqm
Nurqm

Reputation: 4743

You can use * for putting all the values in one variable, this variable will be a list. Then, you can do:

@client.command() 
async def add(ctx, *nums):
    operation = " + ".join(nums)
    await ctx.send(f'{operation} = {eval(operation)}')

@client.command() 
async def sub(ctx, *nums): 
    operation = " - ".join(nums)
    await ctx.send(f'{operation} = {eval(operation)}')

@client.command() 
async def multiply(ctx, *nums): 
    operation = " * ".join(nums)
    await ctx.send(f'{operation} = {eval(operation)}')

@client.command() 
async def divide(ctx, *nums): 
    operation = " / ".join(nums)
    await ctx.send(f'{operation} = {eval(operation)}')

Or you can create just one command named calculate, it'll probably be more useful.

@client.command()
async def calculate(ctx, operation, *nums):
    if operation not in ['+', '-', '*', '/']
        await ctx.send('Please type a valid operation type.')
    var = f' {operation} '.join(nums)
    await ctx.send(f'{var} = {eval(var)}')

With this command, user also has to type the operation type.

Upvotes: 3

Related Questions