Python Question

0 comments

Create a script named monty_hall. This script should import random, and create a class named Simulation.
○ Simulation should take an attribute, an integer, which represents the amount of doors that the simulation
will use. When an instance of Simulation is created it should call the method set_doors using the number
of doors.
○ Set doors will use this number to create a list of “zonk” strings. This list should be as long as the number.
Meaning, if the number passed in is 2, it should create a list containing two “zonks” ([“zonk”,”zonk”]). It
should then replace one of those items in the list to the string “car” at random. It should then pick a
random item from the list which represents the door that the user has chosen. It should then remove a
“zonk” from the list (the order of the zonk doesn’t matter for this part, it can be the first zonk, it can be the
last zonk or it can be a random zonk). It should then pick a random door that the user has not chosen as
the alternate door (similar to Deal or No Deal).
○ Simulation should contain a method named play_game which has 2 keyword arguments, switch
(boolean, default value of False) and iterations (int, default value of 1). Iterations pertains to the amount
of times that the game will be played in this Simulation instance. The goal of this method is to return the
percentage of the amount of times that the game was won as a decimal (float). So for example, if my
iterations is 3 (this means I play the game 3 times) and I win 2 of those 3 times, then my returned value
is .66. If my iterations is 1 and I win 1 time then my returned value is 1. If my iterations is 4 and I dont win
any games then my returned value is 0.
■ The way to determine if a game has been won is to look at whether the door that the player chose
is the “car” and the switch parameter is False or if the alternate door is the “car” and the switch
parameter is True. If those conditions are met then it counts as a win. We will want to keep track
of how many times we win and lose given how many iterations we are running and calculate the
final win percentage.
○ This script should contain an “if name == main” statement where you create an instance of simulation
with 3 doors and you print the returned value from calling the play_game method. This instance should
invoke the play_game method with switch as False and iterations as 100.
■ One note: You may be so inclined to test your code using more iterations, keep in mind, this takes
a lot of computational power. Be weary of adding too many 0s into your script. A sweet spot is
1000. You should notice that your returned value is somewhere near .66 for this instance.
● Create a script named visualization. This script should import monty_hall, pandas and seaborn. This script
should create a class named Plot.
○ Plot should have 2 keyword parameters, doors (int) and simulations (int). Doors represents the amount of
doors that a simulation instance will use simulations pertains to the amount of simulation instances that
we will create. Plot should also create an attribute named sequence that will be a list that will eventually
contain dictionaries that we will use to create a dataframe later. Plot should then contain a loop that will
run for as many times as the value of simulations. Starting at 1, the loop will determine if the current
iteration is odd or even. If even the loop should create an instance of simulation from the monty_hall
namespace and invoke the play_game method. It will pass in the number of doors when created, then
pass in switched as True and iterations will be passed in using the current iteration that we are on. If the
current iteration number is odd then it will do the same thing but it will create an instance of simulation
where the switched value is False. Using the returned percentage, the loop will append a dictionary to
the sequence attribute where the keys are “iterations” (what our current iteration is), “percentage” (our
returned percentage value), “doors” (how many doors our simulation uses) and “switched” (our boolean
as a string representing whether we switched the door or not. After the loop is completed we will call the
make_plot method by passing in the sequence attribute.
■ For example: Lets say that my simulations parameter is 3. I will start at 1 and create that
simulation instance where my iterations argument for the play_game method is 1 and append that
dictionary to the list. I will then go to 2 and do the same where the iterations argument is 2. I will
then go to 3 and do the same where the iterations argument is 3 and stop.
○ Plot should contain a method named make_plot. This method will take an argument, named sequence
which it will use in order to create a pandas Dataframe. We will then use the lmplot method of seaborn in
order to create a plot object where x is “iterations”, y is “percentage”, data is the pandas dataframe that
we created and “hue” is “switched”. You should then use the savefig method of the plot object in order to
export a png of the visualization. The name of your png file should include the simulations attribute and
the doors attribute.
○ This script should contain an “if name == main” statement which will call an instance of Plot by passing in
doors as 5 and simulations as 100.
● Write docstrings for each class and method

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}