Search code examples
pythonrandomdice

Is there a way to determine successes in a python dice roller?


I am new to programming (like very new) and I decided to make a dice roller for my favorite RPG, Vampire the Masquerade. For those who don't know, when you want to make a roll in VtM, you make dice pools of d10s (ten sided dice) and then whether you succeed or not is based on the amount of "successes" you rolled; any dice result of 6 or more.

So far, i've managed to make a basic roller that just rolls d10s:

#"d" is the number of dice
def roll(d):
    return [randint(1,10) for i in range(d)]

#Main Program
d = int(input("\nHow many dice are you rolling? > "))
results = roll(d)
print(results)

However, I'm having trouble making the program count the number of successes in the roll. I was hoping for the output to look something like

How many dice are you rolling? > 2
[5, 9]
1 success

Any advice?


Solution

  • Just do:

    d = int(input("\nHow many dice are you rolling? > "))
    results = roll(d)
    print(results)
    x = sum(i >= 6 for i in results)
    print([f'{x} success', 'failure'][not x])
    

    Also the second last line could become:

    x = sum(1 for i in results if i >= 6)