Advertisement
dcrogers1981

d20-Shared Script rev.1

Sep 22nd, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. from random import randint
  2.  
  3. def dice_roll(dice_to_roll, find_max=False, find_min=False):
  4.  
  5.     """Rolldice for a d20 game, such as D&D
  6.         NOTES:      Setting find_max and find_min to True will return the max. It also explains why HAL killed the crew.
  7.         ACCEPTS:    dice_to_roll: str, representation of the dice roll, like '2d10+3'
  8.                     find_max: bool (False), set to True to return the maximum sum
  9.                     find_min: bool (False), set to True to return the minimum sum
  10.         RETURNS:    dice_roll: int, the sum of the rolled dice
  11.     """
  12.  
  13.     times = 0
  14.     sides = 0
  15.     additional = 0
  16.     if '+' in dice_to_roll:
  17.         additional = int(dice_to_roll.split('+')[1])
  18.         times, sides = dice_to_roll.split('d')
  19.         times = int(times)
  20.         sides = int(sides.split('+')[0])
  21.     else: times, sides = dice_to_roll.split('d')
  22.     times = int(times)
  23.     sides = int(sides)
  24.     if find_max:
  25.         dice_roll = (times * sides) + additional
  26.     elif find_min:
  27.         dice_roll = sum([randint(1, sides) for _ in range(times)]) + additional
  28.     return dice_roll
  29.    
  30. while True:
  31.     response = input('Enter your dice roll as "2d10+3" or "1d6". [Q to Quit]: ')
  32.     if response.lower().startswith('q'):
  33.         print('See you later minion.')
  34.         break
  35.     print('Your roll "{}" resulted in {}'.format(response, dice_roll(response)))
  36.  
  37.  
  38.  
  39.  
  40. SYNTAX:
  41. dcrogers@LittleLight:~/Documents/Projects/Python/learn$ python3 d20.py
  42. Enter your dice roll as "2d10+3" or "1d6". [Q to Quit]: 1d6
  43. Traceback (most recent call last):
  44.   File "d20.py", line 35, in <module>
  45.     print('Your roll "{}" resulted in {}'.format(response, dice_roll(response)))
  46.   File "d20.py", line 28, in dice_roll
  47.     return dice_roll
  48. UnboundLocalError: local variable 'dice_roll' referenced before assignment
  49. dcrogers@LittleLight:~/Documents/Projects/Python/learn$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement