I'm new in this field. I have been given a task. But my solution differs from the one given. Maybe someone has an idea what it is?
Task :Create 1000 throws with three dices. Save the results in a dictionary.The keys are the numbers 3 to 18. The values the number of throws.Create output as shown.
My solution:
from random import randint# Function to throw three dice and return the total sumdef throw_three_dice(): return sum(randint(1, 6) for _ in range(3))# Dictionary to store the counts of each sumthrows = {i: 0 for i in range(3, 19)}# Perform 1000 throwsfor _ in range(1000): total = throw_three_dice() throws[total] += 1# Find the maximum count for scaling the histogrammax_count = max(throws.values())# Print the histogramprint("Throwing Dices Simulator")print("### Results ###")for total, count in sorted(throws.items()): print(f"{total:02d}: {count:3d} {'#' * int(count / max_count * 50)}"
Output:
Throwing Dices SimulatorResults 03: 4 #04: 19 #######05: 26 ##########06: 64 ########################07: 72 ###########################08: 93 ####################################09: 102 #######################################10: 129 ##################################################11: 111 ###########################################12: 115 ############################################13: 99 ######################################14: 69 ##########################15: 54 ####################16: 27 ##########17: 12 ####18: 4 #
Solution from teacher:
Throwing Dices Simulator: Best Version18: 52 ##17: 149 ########16: 292 ################15: 450 ########################14: 699 ######################################13: 984 ######################################################12: 1177 #################################################################11: 1255 #####################################################################10: 1261 ######################################################################09: 1132 ##############################################################08: 963 #####################################################07: 704 #######################################06: 427 #######################05: 288 ###############04: 129 #######03: 38 ##
I don't understand why the numbers are so different.