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

15 statements  

1""" 

2dice.py 

3 

4implements the dice command 

5""" 

6 

7import random 

8from error import ArgumentRequiredError, UnknownArgumentError 

9 

10async def dice(*args): 

11 """ 

12 dice() 

13 

14 rolls dice 

15 """ 

16 

17 # fix args 

18 args = args[1:] 

19 

20 # check for argument 

21 if len(args) == 0: 

22 raise ArgumentRequiredError 

23 if len(args) > 1: 

24 raise UnknownArgumentError 

25 

26 # get dice 

27 dice_x = int(args[0].split('d')[0]) 

28 dice_y = int(args[0].split('d')[1]) 

29 

30 # check dice 

31 assert 0 < dice_x < 100 

32 assert 0 < dice_y < 100 

33 

34 # roll dice 

35 dies = [random.randint(1, dice_y) for _ in range(dice_x)] 

36 

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 ]) 

45 

46 # return result 

47 return f"```{string}```"