Monty Hall and the Python #3: Collecting results from several simulations

Published July 27, 2021, 1:02 p.m.

Welcome back!  Last time we completed our interactive Monty Hall Problem simulation.  We got it to work just like we had expected:  We are presented with 3 choices;  We make our choice.  Monty Hall reveals a wrong choice.  We have the option to switch our choice.  The correct answer is revealed. 

Now the thing to do is to run our simulation several times and see what kind of results we get.  To do that we will put our simulation into a loop and tally how often we were correct.

To do this very simply we will start by puttting our simulation into a loop. At the top of your code, just below our list of choices we will add a 'for' loop:

for i in range(10):

    correct_choice = sample(choices, 1)[0]

    wrong_choices = [c for c in choices if c != correct_choice]
    ...

I have started with a loop over 10 times.  In Notepad++ you may simply select all of the code that you want in the loop and 'tab' it over.  Likewise. if you need to 'tab' it backward, you can select it all and hold shift while tabbing it back.  (Note:  Python will abide actual 'tab' spacing or 'space-bar' spacing.  Only, it must be consistent.  Don't mix and match spacing types. -- Also, Notepad++ can be configured to insert 4 spaces when the tab is used, if desired.)

That's it!  Now lets save it and run it.  Mine worked ok.  How did yours do?  Make sure it's working at this point.

However, we have not yet created a tally to keep track of how often we were correct!  That can be rectified as follows.  At the top of the code, before the loop begins define a tally variable equal to 0.

tally = 0

Then, at the bottom of our loop, when we used logic to test if our 'new_answer' was the same as the 'correct_answer' we add 1 to our tally (if correct!).

if new_answer == correct_choice:
    how_did_you_do = "correct"
    tally +=1

The '+='  syntax is the python-way of saying tally = tally +1.

Finally, on the outside of our loop we can display our tally.

    ...
    how_did_you_do = "incorrect"
    if new_answer == correct_choice:
        how_did_you_do = "correct"
        tally +=1
        
    print(f"Correct choice was {correct_choice}.")
    print(f"My choice was {new_answer} and I was {how_did_you_do}.")
    
print(f'You were right {tally} out of 10 times.')

Notice that the loop is  one 'tab' or 4 spaces over to the right and the final print of the tally is all the way to the left-- not in the loop.

I would suggest that you play with this until you are comfortable understaning what it is doing.  Next time we will automate some of our responses so that it is no longer 'interactive'  however, we can then run it many more times and gather statistics on our outcomes rather quickly.

Thanks for watching!  See you in the next tutorial!

skip_nextMonty Hall and the Python #4: Automating responses and running 10,000 times
  • Monty Hall and the Python #1: Setting up the Monty Hall Problem

  • Monty Hall and the Python #2: Testing out our simulation

  • Monty Hall and the Python #3: Collecting results from several simulations
    (currently viewing)
  • Monty Hall and the Python #4: Automating responses and running 10,000 times

  • Monty Hall and the Python #5: Expand and Understand; Monty Hall with 100 doors!

  • Monty Hall and the Python #6: Name those doors OR Generate any number of unique strings.