def processCommand(self, whole_command): """ Method: processCommand Interpret a command """ command_split = whole_command.split(" ") command_word = command_split[0] if command_word == "roll": if len(command_split) == 2: roller_string = command_split[1] roller = Roller(roller_string) return roller.roll() else: raise ClindException( "Error: roll requires exactly one argument") elif command_word == "exit": pass elif command_word == "name": with open("src/names.txt") as names_file: names = list(names_file) return random.choice(names).strip() + " " + random.choice( names).strip() elif command_word == "info": if len(command_split) == 2: topic = command_split[1] endpoint = Info.topics[topic] return Info(endpoint).listResults() else: return Info.listTopics() else: raise ClindException( "Error: command {} not recognized".format(command_word))
def game(): prompt = '' while prompt != 'n': prompt = input('Would you like to roll some dice? y/n ') if (prompt.lower() == 'y'): dice_str = input('Enter a dice value in the format XdY\n' ' where x is the number of die and\n' ' y is the number of sides per die\n') pattern = re.compile(r'\d+d\d+') if pattern.match(dice_str) != None: dice = dice_str.split('d') dice_sides = abs(int(dice[1])) num_dice = abs(int(dice[0])) if (dice_sides < 100 and num_dice < 100): roller = Roller(num_dice, dice_sides) print('Rolling . . .\n') sleep(1) roller.roll() print('Here are your rolls\n') print(roller) else: print('Your values are too big, try again') else: print('Try again. The format is XdY')