Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import random
- def parse_input(string):
- console_pattern = "(\+|\-)?\s*(\d*d\d*|\d*$)"
- result = re.findall(console_pattern, string)
- if result == [('','')]:
- return None
- else:
- result.pop()
- die_pattern = "\d*d\d*"
- total_roll_sum = 0
- total_roll_history = []
- for x in result:
- if re.search(die_pattern, x[1]):
- dice = x[1].split('d')
- roll_sum = 0
- roll_history = []
- for y in range(int(dice[0])):
- roll = random.randint(1, int(dice[1]))
- roll_sum += roll
- roll_history.append(roll)
- else:
- roll_sum = int(x[1])
- if x[0] == '-':
- roll_sum *= -1
- total_roll_sum += roll_sum
- total_roll_history.append(roll_history)
- return [total_roll_sum, total_roll_history]
- print parse_input("3d5 + 4d3 - 2d8")
Add Comment
Please, Sign In to add comment