Last active
June 6, 2019 18:39
-
-
Save obestwalter/a8fded81b3fee3b9780254b322531cd3 to your computer and use it in GitHub Desktop.
Is this too much for new programmers already?
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
"""DICE ROLLER | |
By Al Sweigart al@inventwithpython.com | |
Example input: | |
3d6 rolls three 6-sided dice | |
1d10+2 rolls one 10-sided dice, and adds 2 | |
2d17-1 rolls two 17-sided dice, and subtracts 1 | |
QUIT quits the program | |
""" | |
import random | |
import sys | |
def main(): | |
print(__doc__) | |
while True: | |
try: | |
roll_the_dice() | |
except KeyboardInterrupt: | |
sys.exit("bye :)") | |
except Exception as exc: | |
# Catch any raised exceptions and display the message to the user: | |
print( | |
'Invalid input. Enter something like `3d6` or `1d10+1`\n' | |
'Input was invalid because: ' + str(exc) | |
) | |
continue | |
def roll_the_dice(): | |
diceStr = input('> ') # The prompt to enter the dice string. | |
if diceStr.upper() == 'QUIT': | |
sys.exit() | |
# Clean up the dice string by removing spaces, converting to lowercase: | |
diceStr = diceStr.lower().replace(' ', '') | |
# Find the number of dice to roll: | |
dIndex = diceStr.find('d') | |
if dIndex == -1: | |
raise Exception('Missing the "d" character.') | |
# Get the number of dice: | |
numberOfDice = diceStr[:dIndex] # The `3` in `3d6+1`. | |
if not numberOfDice.isdecimal(): | |
raise Exception('Missing the number of dice.') | |
numberOfDice = int(numberOfDice) | |
# Find if there is a plus or minus sign: | |
modIndex = diceStr.find('+') | |
if modIndex == -1: | |
modIndex = diceStr.find('-') | |
# Find the number of sides: | |
if modIndex == -1: | |
numberOfSides = diceStr[dIndex + 1:] # The `6` in `3d6+1`. | |
if not numberOfSides.isdecimal(): | |
raise Exception('Missing the number of sides.') | |
numberOfSides = int(numberOfSides) | |
modAmount = 0 | |
else: | |
numberOfSides = diceStr[dIndex + 1:modIndex] | |
if not numberOfSides.isdecimal(): | |
raise Exception('Missing the number of sides.') | |
numberOfSides = int(numberOfSides) | |
# Find the modifier amount: | |
modAmount = int(diceStr[modIndex + 1:]) # The `1` in `3d6+1`. | |
if diceStr[modIndex] == '+': | |
pass # Do nothing if it's +. | |
elif diceStr[modIndex] == '-': | |
# Change the modification amount to negative: | |
modAmount = -modAmount | |
# Simulate the dice rolls: | |
rolls = [] | |
for i in range(numberOfDice): | |
rolls.append(random.randint(1, numberOfSides)) | |
# Display the total: | |
print(sum(rolls) + modAmount, '(', end='') | |
# Display the individual rolls: | |
for i, roll in enumerate(rolls): | |
rolls[i] = str(roll) | |
print(', '.join(rolls), end='') | |
# Display the modifier amount: | |
if modAmount != 0: | |
print(', %s%s' % (diceStr[modIndex], abs(modAmount)), end='') | |
print(')') | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment