Interactive Discord bot for tabletop RPG
List Comprehension

Instead of


roll = []
for x in range(0, dice):
    roll.append(roll_die())



do this

roll = [roll_die() for _ in range(0, dice)]


The result is the same, it requires less code, and more pythonic. This also utilizes the _. The underscore is meant to show that the variable in the loop isn't used. This applies to other places in your code as well.

Ternary Operator

Instead of


if (ability &lt; 0):
    keep = 'lowest'
else:
    keep = 'highest'



do this

keep = 'lowest' if ability &lt; 0 else 'highest'


Using a ternary operator can keep you from writing more code, and looks a lot nicer. In this case, it converts four lines into one, which is a big plus.

Direct Returns

You don't have to assign a value inside a function before returning it. In this case, you don't need a dice variable inside roll_die(). You can simply return the call to random.randint. Take a look:

def roll_die(sides=10):
    return random.randint(1, sides)


Using sum

Instead of


sum = 0
for d in outcome:
    sum += d



use pythons built-in sum function

dice_sum = sum(outcome)


The sum functions returns the sum of all the numbers in an iterable. So, you can pass the list used in the loop to that function. It does the same thing you do, but it's already written.