Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- def dice_roll(dice_to_roll, find_max=False, find_min=False):
- """Rolldice for a d20 game, such as D&D
- NOTES: Setting find_max and find_min to True will return the max. It also explains why HAL killed the crew.
- ACCEPTS: dice_to_roll: str, representation of the dice roll, like '2d10+3'
- find_max: bool (False), set to True to return the maximum sum
- find_min: bool (False), set to True to return the minimum sum
- RETURNS: dice_roll: int, the sum of the rolled dice
- """
- times = 0
- sides = 0
- additional = 0
- if '+' in dice_to_roll:
- additional = int(dice_to_roll.split('+')[1])
- times, sides = dice_to_roll.split('d')
- times = int(times)
- sides = int(sides.split('+')[0])
- else: times, sides = dice_to_roll.split('d')
- times = int(times)
- sides = int(sides)
- if find_max:
- dice_roll = (times * sides) + additional
- elif find_min:
- dice_roll = sum([randint(1, sides) for _ in range(times)]) + additional
- return dice_roll
- while True:
- response = input('Enter your dice roll as "2d10+3" or "1d6". [Q to Quit]: ')
- if response.lower().startswith('q'):
- print('See you later minion.')
- break
- print('Your roll "{}" resulted in {}'.format(response, dice_roll(response)))
- SYNTAX:
- dcrogers@LittleLight:~/Documents/Projects/Python/learn$ python3 d20.py
- Enter your dice roll as "2d10+3" or "1d6". [Q to Quit]: 1d6
- Traceback (most recent call last):
- File "d20.py", line 35, in <module>
- print('Your roll "{}" resulted in {}'.format(response, dice_roll(response)))
- File "d20.py", line 28, in dice_roll
- return dice_roll
- UnboundLocalError: local variable 'dice_roll' referenced before assignment
- dcrogers@LittleLight:~/Documents/Projects/Python/learn$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement