【问题标题】:How do i roll mutiples dices in one command如何在一个命令中滚动多个骰子
【发布时间】:2021-11-13 07:56:12
【问题描述】:

我正在制作一个不和谐的机器人,我已经制作了一个掷骰子系统,但我想改进它。我想一次掷多个骰子,就像 Avrae 一样。

@client.command(aliases=['r', 'dado', 'dice'])
async def roll(ctx, dados='', numero=20, conta='', ficha=''):
  rolagem = random.randint(1,int(numero))
  if conta == '':
        total = (int(rolagem))
  elif conta == '+':
         total = (int(rolagem) + int(ficha))
  elif conta == '-':
         total = (int(rolagem) - int(ficha))
  elif conta == 'x':
         total = (int(rolagem) * int(ficha))
  elif conta == '/':
         total = (int(rolagem) / int(ficha))
  if ficha == '':
          ficha = ''
  if total < 0:
          total = '1'
  if total == 0:
          total == '1'
  if rolagem == 20:
          rolagem = '**20**'
  if rolagem == 1:
          rolagem = '**1**'
  await ctx.send(f'{ctx.author.mention} ???? \n**Resultado**: D{numero} ({rolagem}) {conta} {ficha}\n**Total**: {total}')

因此,该命令应该像:(前缀)r(要掷骰子的数量)(骰子),并显示如下结果:(掷骰子的数量):(骰子结果)(骰子结果的总和) 例如:-r 5 d20; 5 D20 的结果:(1、5、8、16、20)(总和)。 我想知道我应该怎么做

【问题讨论】:

  • 这很简单。您有 1 个案例,现在您需要解决 n 案例。考虑使用循环包装您的代码(例如,运行n 次的 for 循环),将结果添加到结果列表中,然后返回该列表的总和。

标签: python discord discord.py


【解决方案1】:

这是我写的一个滚动函数,修改为像你一样使用字符串:

import random


def roll(n='1', d='20', a='+', m='0'):
    res = []
    for i in range(int(n)):
        res.append(random.randint(1, int(d)))

    roll_modified = sum(res)
    if a == '+' or a == '-' or a == '*' or a == '/':
        roll_modified = eval('{}{}{}'.format(roll_modified, a, int(m)))

    if roll_modified < 1:
        roll_modified = 1

    return roll_modified, res

它使用eval 来应用修饰符,因此请谨慎使用。我检查运算符的值并在数字上使用int() 以确保它是一个安全的表达式。

像这样使用它:

roll_modified, res = roll('3', '20', '-', '2')
  • roll_modified 是应用 sum 和修饰符后的结果。
  • res 是所有原始卷的列表

然后您可以使用以下命令打印结果:

res_str = ["**{}**".format(x) if x == 20 or x == 1 else str(x) for x in res]
print(roll_modified, "[{}]".format(", ".join(res_str)))

将输出:

36 [14, 4, **20**]

【讨论】:

  • 它没有用... idk 如何从那里添加 anithing 到我的代码
  • 不起作用是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 2013-09-17
  • 1970-01-01
  • 2016-08-10
  • 2023-03-28
  • 2021-06-25
  • 1970-01-01
相关资源
最近更新 更多