import random # Play the rolling game def PlayGame(): rolls = RollDiceSeveralTimes(1000) rollHistogram = GetHistogramFromList(rolls) PrintHistogram(rollHistogram) # Roll two dice a given number of times, # returns a list of results def RollDiceSeveralTimes(times): rolls = list() for roll in range(times): rollResult=RollTwoDice() rolls.append(rollResult) return rolls # Given a list of values, return a dictionary # where the key if the value in of the list # and the value is the number of times that # key shows up in the list def GetHistogramFromList(listOfValues): histogram = dict() for value in listOfValues: histogram[value]=histogram.get(value,1)+1 # .get returns the value if the key exists, else it will return given value 1 1 return histogram def PrintHistogram(histogram): for key in histogram.keys(): print(key,": ",histogram[key]) # Get the sum of rolling two 6 sided die def RollTwoDice(): return RollDie() + RollDie() # Roll a single 6 sided die def RollDie(): return random.randint(1,6) PlayGame()
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