# Michael Burgess (py@mjburgess.co.uk) from itertools import product num_attacking_dice = 3 # A num_defending_dice = 2 # D die = range(1, 7) attacking_rolls = [sum(possibility) for possibility in product(die, repeat=num_attacking_dice)] defending_rolls = [sum(possibility) for possibility in product(die, repeat=num_defending_dice)] results = [attack - defence for attack in attacking_rolls for defence in defending_rolls] num_wins = len(filter(lambda result: result > 0, results)) num_loses = len(filter(lambda result: result < 0, results)) num_draws = len(filter(lambda result: result == 0, results)) num_all = float(len(results)) prob_win = float(num_wins) / num_all prob_draw = float(num_draws) / num_all prob_lose = float(num_loses) / num_all print("Probaility of winning %.1f, of drawing %.1f, of losing %.1f" % (prob_win * 100, prob_draw * 100, prob_lose * 100))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question