Skip to content

Instantly share code, notes, and snippets.

@elliott-king
Created June 18, 2018 19:08
Show Gist options
  • Save elliott-king/1cbb645f3b42cf2916b6fa56261a7d49 to your computer and use it in GitHub Desktop.
Save elliott-king/1cbb645f3b42cf2916b6fa56261a7d49 to your computer and use it in GitHub Desktop.
DailyProgrammer problem #364.
"""
r/DailyProgrammer Challenge #364
Dice Roller
2018 - 06 - 18
https://www.reddit.com/r/dailyprogrammer/comments/8s0cy1/20180618_challenge_364_easy_create_a_dice_roller/?st=jiklnspm&sh=2cffa04e
"""
from random import randint
def identify_dice(dice_string):
i = dice_string.find('d')
if i <= 0 or i == len(dice_string) - 1:
raise ValueError()
return int(dice_string[:i]), int(dice_string[i+1:])
def roll_dice(num, dice_val):
if num < 1:
return 0
r = randint(1, dice_val)
return r + roll_dice(num-1, dice_val)
while(True):
# For this simple program, we will simply use ^C to exit.
dice_string = raw_input("What should I roll?\n")
try:
num, dice_val = identify_dice(dice_string)
print(str(roll_dice(num, dice_val)) + "\n")
except:
print("There seems to have been a problem with your input. To exit, use ^c.\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment