Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatbot/handlers/dice.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
25 lines (19 sloc)
750 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" DiceService and related. """ | |
import random | |
import math | |
from .service import Service | |
class DiceService(Service): | |
""" Service that provides dice related functions. """ | |
def roll_dice(self, sides=6): | |
""" Returns the result of a dice roll. """ | |
accepted = (4, 6, 8, 10, 12, 20, 100, math.inf) | |
if not isinstance(sides, (int, float)): | |
raise TypeError("sides must be numeric.") | |
if sides not in accepted: | |
raise ValueError( | |
("{} is not a valid number of sides. " | |
+ "Valid sides are {}").format(sides, accepted)) | |
# if sides is math.inf, return it | |
if sides == math.inf: | |
return math.inf | |
return random.randrange(1, sides) |