vendredi 23 octobre 2020

Python running a random.randint() multiple times and adding the results together

I'm coding a discord py bot, and I'm not sure how to proceed. What I'm trying to do with this command is make the bot roll by following the xdy format, where x might be 1 and y might be 20, so 1d20. That part, and the arg2 and arg3 for a + x or - x work great. What I'm unsure about is how to have the bot roll as many times as is specified in arg1[0], and add up all the results together instead of multiply the first roll by arg1[0].

I do understand why it multiplies it, but I'm not exactly sure how to make it roll multiple times and add up the results together.

Thank you!

@bot.command(
help = "Roll a D20"
)
async def roll(ctx, arg1=None, arg2=None, arg3=None):
    if arg1:
        r = str(int(arg1[0]) * random.randint(1,int(arg1[2:])))
        
        if arg2 and arg2 == "+":
            args = str.join(str(arg2),str(arg3))
            ar = int(r) + int(args)
            await ctx.channel.send(ctx.author.mention + ", you rolled a " + str(r) + " + " + str(args) + ", so your total is " + str(ar) + "!")
        
        elif arg2 and arg2 == "-":
            args = str.join(str(arg2),str(arg3))
            ar = int(r) - int(args)
            await ctx.channel.send(ctx.author.mention + ", you rolled a " + str(r) + " - " + str(args) + ", so your total is " + str(ar) + "!")
        
        else:
            await ctx.channel.send(ctx.author.mention + ", you rolled a " + str(r) + "!")

    else:
        await ctx.channel.send("Please add the dice values!")



Aucun commentaire:

Enregistrer un commentaire