Coverage for cmds/dice.py: 20%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2dice.py
4implements the dice command
5"""
7import random
8from error import ArgumentRequiredError, UnknownArgumentError
10async def dice(*args):
11 """
12 dice()
14 rolls dice
15 """
17 # fix args
18 args = args[1:]
20 # check for argument
21 if len(args) == 0:
22 raise ArgumentRequiredError
23 if len(args) > 1:
24 raise UnknownArgumentError
26 # get dice
27 dice_x = int(args[0].split('d')[0])
28 dice_y = int(args[0].split('d')[1])
30 # check dice
31 assert 0 < dice_x < 100
32 assert 0 < dice_y < 100
34 # roll dice
35 dies = [random.randint(1, dice_y) for _ in range(dice_x)]
37 # print result
38 string = "\n".join([
39 f"dice: {dies}",
40 f"sum: {sum(dies)}",
41 f"min: {min(dies)}",
42 f"max: {max(dies)}",
43 f"avg: {sum(dies) / len(dies)}",
44 ])
46 # return result
47 return f"```{string}```"