-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
46 lines (36 loc) · 1.15 KB
/
main.py
File metadata and controls
46 lines (36 loc) · 1.15 KB
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
import re
import random
import operator as op
ops = {
'+': op.add,
'-': op.sub,
'*': op.mul,
'/': op.truediv,
'%': op.mod
}
pattern = r"^(\d*)[dD](\d+)([+\-*/%])?(\d+)?$"
while True:
user_input = input("🎲 Enter roll (e.g., 2d6+1) or 'q' to quit: ").strip()
if user_input.lower() in ['q', 'quit']:
break
match = re.match(pattern, user_input)
if not match:
print("⚠️ Invalid input. Try something like '2d6+1' or 'd20'.")
continue
groups = match.groups()
num_dice = int(groups[0]) if groups[0] else 1
faces = int(groups[1])
operator = groups[2]
modifier = int(groups[3]) if groups[3] else 0
print(f'\nRolling {num_dice}d{faces} {operator or ""} {modifier if operator else ""}...')
dice_results = []
for _ in range(num_dice):
dice_roll = random.randint(1, faces)
dice_results.append(dice_roll)
print(f'🎯 Rolls: {dice_results}')
sum_result = sum(dice_results)
if operator and groups[3]:
final_result = ops[operator](sum_result, modifier)
else:
final_result = sum_result
print(f'🧮 Final Result: {final_result}\n')