Guest User

Untitled

a guest
Jun 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import re
  2. import random
  3.  
  4. def parse_input(string):
  5. console_pattern = "(\+|\-)?\s*(\d*d\d*|\d*$)"
  6.  
  7. result = re.findall(console_pattern, string)
  8.  
  9. if result == [('','')]:
  10. return None
  11. else:
  12. result.pop()
  13.  
  14. die_pattern = "\d*d\d*"
  15.  
  16. total_roll_sum = 0
  17. total_roll_history = []
  18.  
  19. for x in result:
  20. if re.search(die_pattern, x[1]):
  21. dice = x[1].split('d')
  22. roll_sum = 0
  23. roll_history = []
  24. for y in range(int(dice[0])):
  25. roll = random.randint(1, int(dice[1]))
  26. roll_sum += roll
  27. roll_history.append(roll)
  28. else:
  29. roll_sum = int(x[1])
  30.  
  31. if x[0] == '-':
  32. roll_sum *= -1
  33.  
  34. total_roll_sum += roll_sum
  35. total_roll_history.append(roll_history)
  36.  
  37. return [total_roll_sum, total_roll_history]
  38.  
  39. print parse_input("3d5 + 4d3 - 2d8")
Add Comment
Please, Sign In to add comment