# Python 3, May 29, 2023 #Code for drawing a histogram of dice rolls #Eric Rasmusen, erasmuse61@gmail.com """ Homework instructions: Get two six-sided dice. If possible, get two different colors. Throw one to the left and one to the right, five times Record down the left numbers and the right numbers, on paper first or directly to the lists left5_johnny and right5_johnny in the Python code below. Put your own name in instead of "johnny". Then run the Python code, either in Codabrainy or in Miktex if you downloaded it and want to try that. Then roll the dice 20 more times, and record that down in the Python file under left20_johnny and right20_johnny. Then run the Python code again. Then email me your Python code. I will take everybody's dice roll data and combine it so we can see in class what the histogram looks like for 150 data points. """ print("Python 3, May 29, 2023\n Code for drawing a histogram of dice rolls", end = ".\n\n") print("******************************************** ", end = " \n\n") import matplotlib.pyplot as plt left5_johnny = [3,3,5,2,5,] right5_johnny = [3,5,3,1,1] data5a= left5_johnny data5b= right5_johnny data5 = [999 for item in range (0,5)] print("We start out the list data5 to be", data5, end = ".\n\n") #We will write over those 999's next. for item in range(0,5): data5[item] = data5a[item] + data5b[item] print("The list of total dice rolls is ", data5, end = ".\n\n") print("******************************************** ", end = " \n\n") mean = sum(data5)/5 print("The mean is ", mean, end = ".\n\n") data_sorted = sorted (data5) print("The sorted data is ", data_sorted, end = ".\n\n") median = data_sorted[2] #This is 2, not 3, because Python counts 0,1,2,3,4. print("The median is ", median, end = ".\n\n") plot20 = plt.hist(data5, color = 'red', edgecolor = 'black' ) plt.xlabel('A Histogram of Dice Rolls') plt.savefig('johnny5.png') plt.close() #The mode is the bin with the highest bar, the most items in the bin. print("******************************************** ", end = " \n\n") left20_johnny= [4,6,2,5,1, 1,2,6,4,1,2,6,5,2,4,5,3,5,6,4 ] right20_johnny = [ 5,2,4,3,5, 6,2,6,2,3, 4,5,1,6,5, 2,3,3,2,3] data20a= left20_johnny data20b= right20_johnny data20 = [999 for item in range (0,20)] print("We start out the list data20 to be", data20, end = ".\n\n") #We will write over those 999's next. for item in range(0,20): data20[item] = data20a[item] + data20b[item] print("The list of 20 total dice rolls is", data20, end = ".\n\n") print("******************************************** ", end = " \n\n") data25 = data20 + data5 print("The final list of 25 total dice rolls is", data25, end = ".\n\n") print("******************************************** ", end = " \n\n") mean = sum(data25)/25 print("The mean is ", mean, end = ".\n\n") data_sorted = sorted (data25) print("The sorted data is ", data_sorted, end = ".\n\n") median = data_sorted[12] print("The median is ", median, end = ".\n\n") plot25=plt.hist(data25, color = 'blue', edgecolor = 'black' ) plt.xlabel('A Histogram of Dice Rolls') plt.savefig('johnny25.png') plt.close() print("******************************************** ", end = " \n\n") import matplotlib.pyplot as plt #print("************************************** ", end = " \n\n") #Here goes the student data. left5_job = [ 3,1,6,5,1] right5_job = [ 6,1,1,1,5] left20_job = [5,2,3,1,1, 3,4,2,3,4, 3,6,2,6,2, 6,4,6,6,1] right20_job = [1,6,4,2,5, 4,4,5,6,3, 4,2,5,6,3, 2,5,5,5,1,] left25_job = left5_job + left20_job right25_job = right5_job + right20_job #print("The list of 25 left dice rolls for Job is", #left25_job, end = ".\n\n") #print("************************************ ", end = " \n\n") left5_kaeb = [1,6,4,1,1] right5_kaeb = [1,1,3,4,2] left20_kaeb = [2,5,4,1,6,2,5,1,6,2,6,5,3,3,1,2,5,5,2 ] right20_kaeb = [3,4,5,4,5,5,4,6,5,6,1,3,1,1,1,3,4,3,1,2] left25_kaeb = left5_kaeb + left20_kaeb right25_kaeb = right5_kaeb + right20_kaeb #print("The list of 25 left dice rolls for Kaeb is", left25_kaeb, end = ".\n\n") #print("************************************ ", end = " \n\n") left5_tucker = [3,3,5,2,5,] right5_tucker = [3,5,3,1,1] left20_tucker= [4,6,2,5,1,1,2,6,4,1,2,6,5,2,4,5,3,5,6,4 ] right20_tucker = [ 5,2,4,3,5,6,2,6,2,3,4,5,1,6,5,2,3,3,2,3] left25_tucker = left5_tucker + left20_tucker right25_tucker = right5_tucker + right20_tucker #print("The list of 25 left dice rolls for Liam Tucker is", left25_tucker, end = ".\n\n") #print("************************************ ", end = " \n\n") left25_3students = left25_tucker + left25_job + left25_kaeb right25_3students = right25_tucker + right25_job + right25_kaeb data_3students = [item for item in range(len(right25_3students))] #This just creates a list of the correct length to be overwritten later. data_3students = [left25_3students[item] + right25_3students[item] for item in range(-1,len(right25_3students) -1)] # Here is another way to do the FOR loop to create data_3students #for item in range(-1,len(right25_3students)-1): # data_3students[item] = left25_3students[item] + right25_3students[item] # print(data_3students[item]) #print("************************************ ", end = " \n\n") evelyn5 = [6, 10, 10, 4, 7] evelyn25 = [5, 5, 3, 9, 6, 7, 7, 4, 8, 6, 11, 11, 7, 4, 6, 5, 7, 4, 4, 9, 6, 10, 10, 4, 7] student_data100 = data_3students + evelyn25 rolls = str(len(student_data100)) + " rolls" print("length of the data is", rolls, end = ".\n\n") print("************************************** ", end = " \n\n") mean = sum(student_data100 )/len(student_data100) print("The mean with",rolls, "is", mean, end = ".\n\n") data_sorted = sorted (student_data100) #print("The sorted data is ", data_sorted, end = ".\n\n") median = data_sorted[ int(len(student_data100)/2) ] print("The median with", rolls,"is", median, end = ".\n\n") plot=plt.hist(student_data100, bins=30, density="False", color = 'blue', edgecolor = 'red', alpha = .4, rwidth=2.9 ) plt.xlabel("A Histogram of " + rolls) plt.savefig('class100.png') plt.close() #print("************************************** ", end = " \n\n") import random as random rolls = 2000 left_die = [random.randint(1,6) for item in range(0, rolls)] right_die = [random.randint(1,6) for item in range(0, rolls)] data_two_dice = [left_die[item] + right_die[item] for item in range(0,rolls)] mean = sum(data_two_dice )/len(data_two_dice) print("The mean with", rolls, "rolls is", mean, end = ".\n\n") data_sorted = sorted (data_two_dice) #print("The sorted data is ", data_sorted, end = ".\n\n") median = data_sorted[ int(rolls/2) ] print("The median with", rolls,"rolls is", median, end = ".\n\n") plot=plt.hist(data_two_dice, bins=30, density="False", color = 'blue', edgecolor = 'red', alpha = .4, rwidth=2.9 ) temp1 = str(rolls) + " rolls" plt.xlabel("A Histogram of " + temp1) temp2 = str(rolls) + "rolls" + ".png" plt.savefig(temp2) plt.close() print("************************************ ", end = " \n\n") """ EMAIL. If you get Miktex set up, write a program something like this \documentclass{article} \begin{document} Here write something \end{document} See the page in yesterday's handout, "Using LaTeX via Python" for how to put some math expressions such as $x^3 -(\sqrt{4x} + \pi)$ into the file. If you can't get MikTeX to work, use codabrainy and follow the instructions on that same page for how to use LateX in Python. """