-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroller.py
56 lines (34 loc) · 1.17 KB
/
roller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 19 16:34:53 2020
@author: ardunster
"""
#Dice roller script. For D&D and any other game requiring randomized rolls.
#Dice size are not limited to traditional dice. You can roll any die you want.
import random
import datetime
print('Input desired dice roll in xdy format, ie, 1d6.')
dice_input = input('Roll: ')
#Validate format
while (not 'd' in dice_input) or (int(dice_input.count('d')) > 1):
print('Please enter desired dice roll in xdy format.')
dice_input = input('Roll: ')
d = int(dice_input.index('d'))
dice_qty = int(dice_input[:d])
dice_size = int(dice_input[d+1:])
# Roll Dice
roll = []
for i in range(dice_qty):
roll.append(random.randrange(1, dice_size))
#Return results
if dice_qty > 1:
print(f'You rolled {dice_qty}d{dice_size}, resulting in rolls of:')
else:
print(f'You rolled {dice_qty}d{dice_size}, resulting in a roll of:')
print(roll)
print(f'Your total roll is {sum(roll)}.')
#Log results with timestamp
now = datetime.datetime.now()
roll_log = open('roll_log.txt', 'a')
roll_log.write(f'{now}\nRolled {dice_qty}d{dice_size}, resulting in {roll}, total {sum(roll)}.\n')
roll_log.close()