更简单的计算器命令方式?
Easier way of calculator command?
所以这是我的计算器命令,但它需要超过 430 行来计算东西而且它似乎非常低效,你只能用一个运算符计算并且一次只能给出 2 个输入来计算它。
有什么方法可以获取每个输入并用其中的因子进行拆分吗?那么可以一次输入多个输入吗?
@client.command(aliases=['calculate', 'calculator'])
async def calc(ctx, a: int, factor=None, b: int = None):
if factor == None:
await ctx.send(
f"""{ctx.author.mention}, you must include an operation to calculate: ``add`` ``subtract`` ``multiply`` ``divide`` **or** ``+`` ``-`` ``x`` ``/`` **or** ``>`` ``==`` ``<``
eg. &calc 1 (add or +) 100```
eg. &calc 1 (subtract or -) 100```
eg. &calc 1 (multiply or x) 100```
eg. &calc 1 (divide or /) 100```
eg. &calc 1 == 100```
eg. &calc 1 < 100```
eg. &calc 1 > 100```""")
是的,您只需将每个 *args 与提供的运算符连接起来,并连续连接运算符后给定的每个整数即可。
@client.command()
async def calculate(ctx, operation, *nums):
if operation not in ['+', '-', '*', '/']:
await ctx.send('Please use a valid operation type. ``+,-,*,/`` eg. ``calc + 10 10 100``')
return
var = f' {operation} '.join(nums)
await ctx.send(f'{var} = {eval(var)}')
所以这是我的计算器命令,但它需要超过 430 行来计算东西而且它似乎非常低效,你只能用一个运算符计算并且一次只能给出 2 个输入来计算它。
有什么方法可以获取每个输入并用其中的因子进行拆分吗?那么可以一次输入多个输入吗?
@client.command(aliases=['calculate', 'calculator'])
async def calc(ctx, a: int, factor=None, b: int = None):
if factor == None:
await ctx.send(
f"""{ctx.author.mention}, you must include an operation to calculate: ``add`` ``subtract`` ``multiply`` ``divide`` **or** ``+`` ``-`` ``x`` ``/`` **or** ``>`` ``==`` ``<``
eg. &calc 1 (add or +) 100```
eg. &calc 1 (subtract or -) 100```
eg. &calc 1 (multiply or x) 100```
eg. &calc 1 (divide or /) 100```
eg. &calc 1 == 100```
eg. &calc 1 < 100```
eg. &calc 1 > 100```""")
是的,您只需将每个 *args 与提供的运算符连接起来,并连续连接运算符后给定的每个整数即可。
@client.command()
async def calculate(ctx, operation, *nums):
if operation not in ['+', '-', '*', '/']:
await ctx.send('Please use a valid operation type. ``+,-,*,/`` eg. ``calc + 10 10 100``')
return
var = f' {operation} '.join(nums)
await ctx.send(f'{var} = {eval(var)}')