Python has a data structure called lists. They are extremely useful for manipulating data and the data type comes with methods that make it easy for you to do complex operations. The syntax used to represent lists in python are brackets. The example below would represent a list containing 3 elements of 1, 2 and 3.
[1, 2, 3]
So lets dive into the actual code where we make a very useful program which will tell us the probability of obtaining a sum given two dice. So lets say we want to know the probability of getting a sum of 8 with 2 dice well we can do that in python using lists.
Some of the methods we are going to use include sorted, append, reverse, values and Counter which is being imported from collections. The way to think of lists in Python as one of my computer science friend nicely put it is putting stuff into a container.
#!/usr/bin/env python
import sys, os, math
from collections import Counter
def display_possibilities(outcomes): # This does all the printing.
possibility = Counter(outcomes) # Contains the number of possible outcomes for each outcome.
total_outcomes = 0 # This will become 36 once all of the outcomes are added.
for i in possibility.values(): # adding up all of the number of possible outcomes for each outcome.
total_outcomes += i
print("Total Possibilities:", total_outcomes, "\n")
print("Sum: Percentage: Out of", total_outcomes, ":", sep='') # The sep='' just removes whitespace don't let that confuse you.
for i in sorted(Counter(outcomes)):
print(i, " ", round(((possibility[i]/float(total_outcomes)) * 100), 3), "% ", possibility[i])
def main():
highest_roll = 7 # The highest value that the dice roll can have. It is 7 because range(1,7) will
lowest_roll = 1 # The lowest value the dice can roll.
possible_outcomes = [] # this is an empty list
sum_of_outcomes = []
d = [[x] for x in range(lowest_roll ,highest_roll)] # Creates a list of possible values for a dice roll.
rd = [[x] for x in reversed(range(lowest_roll, highest_roll))] # Reverses the list of possible values.
for x in d: # A for loop for putting both lists together creating a list of possible values for both dice rolls.
for i in rd:
possible_outcomes.append(x+i)
for result in possible_outcomes: # Adds the sum of the dice rolls.
sum_of_outcomes.append(result[0]+result[1])
print(sum_of_outcomes, "\n")
display_possibilities(sum_of_outcomes) # Calls the function to print everything.
if __name__=="__main__":
main()
The result of running the code:
[7, 6, 5, 4, 3, 2, 8, 7, 6, 5, 4, 3, 9, 8, 7, 6, 5, 4, 10, 9, 8, 7, 6, 5, 11, 10, 9, 8, 7, 6, 12, 11, 10,
9, 8, 7]
Total Possibilities: 36
Sum: Percentage: Out of36:
2 2.778 % 1
3 5.556 % 2
4 8.333 % 3
5 11.111 % 4
6 13.889 % 5
7 16.667 % 6
8 13.889 % 5
9 11.111 % 4
10 8.333 % 3
11 5.556 % 2
12 2.778 % 1
Usually I would go line by line when explaining each part of the code but I think it would be more useful to start from the main function.highest_roll = 7 # The highest value that the dice roll can have. It is 7 because range(1,7) will
lowest_roll = 1 # The lowest value the dice can roll.
possible_outcomes = [] # this is an empty list
sum_of_outcomes = []
This first part of the main function is setting up all the variables. The highest and lowest roll will go into the range function which will than generate a list. Range is a rather useful function to generate lists and than do manipulations on the list generated by range. Both possibleoutcomes
and sumof__outcomes
are variables holding empty lists waiting to be filled.
d = [[x] for x in range(lowest_roll ,highest_roll)] # Creates a list of possible values for a dice roll.
rd = [[x] for x in reversed(range(lowest_roll, highest_roll))] # Reverses the list of possible values.
d and rd are lists. In fact you can print d and rd and you'll see that rd is just the reverse of d which is what reversed does to a list. You may have noticed that there is a for in the bracket which is just saying for every element in the list created by range put it into the list.for x in d: # A for loop for putting both lists together creating a list of possible values for both dice rolls.
for i in rd:
possible_outcomes.append(x+i)
Another for loop within a for loop. So using this for loop it is taking an element from d and an element and adding it with every element in rd and than moving on like this till every element in d is exhausted. This will result in all the possible outcomes.for result in possible_outcomes: # Adds the sum of the dice rolls.
sum_of_outcomes.append(result[0]+result[1])
Finally one last for loop using the list just created and adding the pairs created in the previous for loop to have a sum of every possible combination that can be made. display_possibilities(sum_of_outcomes) # Calls the function to print everything.
Calls the display Possibilities function that will do all the printing and manipulating of our lists.possibility = Counter(outcomes) # Contains the number of possible outcomes for each outcome.
total_outcomes = 0 # This will become 36 once all of the outcomes are added.
the function Counter is imported from collections and does exactly what it sounds like it counts the number of times a element appears in a list. The total outcome is 0 for now because in the next line there is a for loop that adds up the total outcomes.for i in possibility.values(): # adding up all of the number of possible outcomes for each outcome.
total_outcomes += i
The method .values() works on taking out the values only and then the for loop we add all the values up so we can find the total number of possible outcomes.for i in sorted(Counter(outcomes)):
print(i, " ", round(((possibility[i]/float(total_outcomes)) * 100), 3), "% ", possibility[i])
Finally this prints all of the information out formated in a nice way. The round function that is being used here allows me to select the amount of significant figures I want after the decimal point. The round function takes two arguments one being the number and the other being the number of significant figures. In this case I wanted 3 significant figures after the decimal point and so I put in 3 as the second argument and the first argument is the possibility divided by the total outcome. Notice that I made the total outcome a float in order to get a float answer.