Need help creating a vertical histogram with "#" for the number of rolls I get. It will have a max of 80 characters. This is what I have so far. I can get everything to print the way I need it to except I'm having a lot of trouble with my histogram.
import random
from collections import defaultdict
def main():
dice = int(input("Enter the number of dice: "))
sides = int(input("Enter the number of sides: "))
rolls = int(input("Enter the number of rolls to simulate: "))
result = roll(dice, sides, rolls)
for i in range(dice, dice * sides + 1):
print('{:2d}{:10,d}{:10.4%}'.format(i, result[i], result[i] / rolls))
print(histogram(result, dice, sides, rolls))
def roll(dice, sides, rolls):
d = defaultdict(int)
for i in range(rolls):
d[sum(random.randint(1, sides) for i in range(dice))] += 1
return d
def histogram(result, dice, sides, rolls):
maxBar = str(80)
for x in result:
p = str(x)
if p <= maxBar:
p += '#'
return p
main()
Output example::
5 7 0.0070%
6 64 0.0640%
7 191 0.1910% #
8 429 0.4290% ###
9 942 0.9420% #######
10 1,629 1.6290% ############
11 2,701 2.7010% #####################
12 3,911 3.9110% ###############################
13 5,375 5.3750% ##########################################
14 6,849 6.8490% ######################################################
15 8,383 8.3830% ##################################################################
16 9,371 9.3710% ##########################################################################
17 10,051 10.0510% ################################################################################
18 9,972 9.9720% ###############################################################################
19 9,453 9.4530% ###########################################################################
20 8,371 8.3710% ##################################################################
21 7,022 7.0220% #######################################################
22 5,517 5.5170% ###########################################
23 3,824 3.8240% ##############################
24 2,586 2.5860% ####################
25 1,661 1.6610% #############
26 936 0.9360% #######
27 462 0.4620% ###
28 195 0.1950% #
29 78 0.0780%
30 20 0.0200%
I personally would do something like this: