{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Win a car\n", "\n", "***\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Purpose\n", "\n", "***\n", "\n", "In this notebook you'll simulate playing a simple game of chance in which you are asked to make two choices.\n", "At the end, you'll be asked to submit the outcome of the game.\n", "The outcome will be combined with the outcomes of your classmates and analysed in class.\n", "Please don't Google the game before you get through the whole notebook and submit your game." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### Running this notebook\n", "\n", "***\n", "\n", "This is a Jupyter notebook.\n", "You should download it and run it through Jupyter on your own machine.\n", "The notebook should be run in one session from top to bottom.\n", "If you mess up at any stage, you can click `Restart & Clear Output` from the Kernel menu at the top.\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Background\n", "\n", "***\n", "\n", "The game we're going to play is adapted from an old TV show.\n", "The contestant, after going through a number of rounds, is given the opportunity to win a car.\n", "They're presented with three closed doors: A, B and C.\n", "They're told that a car has been randomly placed behind one of these doors.\n", "The other two doors each have a goat behind them.\n", "All the contestant has to do is pick a door to open.\n", "\n", "\n", "\n", "\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Starting variables\n", "\n", "***\n", "\n", "To begin, I'd like you to set each of the following three variables to either `True` or `False` in the cell below.\n", "Please don't Google anything for now - just use the information currently in your head.\n", "Remember, this is just a bit of fun.\n", "\n", "\n", "- Set `einstein` to `True` if you have ever heard of someone called Albert Einstein, `False` otherwise.\n", "- Set `monty` to `True` if you have ever heard of someone called Monty Hall, `False` otherwise.\n", "- Set `savant` to `True` if you have ever heard of someone called Marilyn vos Savant, `False` otherwise.\n", "\n", "So, if you know who Einstein is but not Monty Hall or Marilyn vos Savant then type the following in the cell below and press Shift+Enter.\n", "\n", "```python\n", "einstein = True\n", "monty = False\n", "savant = False\n", "```\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Set these variables as described above.\n", "einstein = \n", "monty = \n", "savant = " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### Quick check 1\n", "\n", "***\n", "\n", "Run the cell below to make sure everything at this point is running okay.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Assume everything is running okay.\n", "aok = True\n", "\n", "# Make sure the einstein variable is set to True or False.\n", "try:\n", " if einstein not in [True, False]:\n", " print(\"Error: please set the einstein variable to either True or False!\")\n", " aok = False\n", "except NameError:\n", " print(\"Error: please set the einstein variable to either True or False!\")\n", " aok = False\n", "\n", "# Make sure the monty variable is set to True or False.\n", "try:\n", " if monty not in [True, False]:\n", " print(\"Error: please set the monty variable to either True or False!\")\n", " aok = False\n", "except NameError:\n", " print(\"Error: please set the monty variable to either True or False!\")\n", " aok = False\n", "\n", "# Make sure the savant variable is set to True or False.\n", "try:\n", " if savant not in [True, False]:\n", " print(\"Error: please set the savant variable to either True or False!\")\n", " aok = False\n", "except NameError:\n", " print(\"Error: please set the savant variable to either True or False!\")\n", " aok = False\n", "\n", "# If there are no problems, print a statement to continue.\n", "if aok:\n", " print(\"Quick Check 1: everything is running okay, please continue.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### Beginning the game\n", "\n", "***\n", "\n", "Below is some Python code to generate a random choice between three doors as pictured above: red, green and blue.\n", "The code will randomly choose one of the doors to put the car behind.\n", "There's no cheating here - the car is as likely to be behind any one of the doors.\n", "\n", "The colour of the door with the car is stored as a string in the variable `car`.\n", "The red door is indicated by the string `'red'`, the green door by `'green'` and the blue door by `'blue'`.\n", "Please don't look at the value of the `car` variable, at least until the game is over.\n", "If you re-run the cell you will again randomly select a door - possibly the same, possibly different.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Python provides a library called random to generate pseudo-random numbers.\n", "import random\n", "\n", "# The three doors in a list.\n", "doors = ['red', 'green', 'blue']\n", "\n", "# Pick a random door.\n", "car = random.choice(doors)\n", "\n", "# Tell the user we've picked.\n", "print(\"From Python: I've randomly selected a door to put the car behind.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### Your first choice\n", "\n", "***\n", "\n", "In the cell above, Python randomly selected a door to put the car behind (implying there are goats behind the other two doors.)\n", "You shouldn't have looked at what Python chose - it's stored in the `car` variable.\n", "\n", "Here is your first of two choices as the contestant: pick one of the doors that you think the car might be behind.\n", "To indicate your preference, in the cell below set the variable `pick` to `'red'` if you want to pick the red door, `'green'` for the green door or `'blue'` for the blue.\n", "\n", "Here you are being asked to guess what door Python might have chosen, based on no information really - it's just a guess.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Pick one of three doors: 'red', 'green' or 'blue'.\n", "pick = " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### Quick check 2\n", "\n", "***\n", "\n", "Run the cell below to make sure everything at this point is running okay.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Assume everything is running okay.\n", "aok = True\n", "\n", "# Make sure car is set to a door.\n", "try:\n", " if car not in doors:\n", " print(\"Error: please set the car variable to one of 'red', 'green' or 'blue'!\")\n", " aok = False\n", "except NameError:\n", " print(\"Error: please set the car variable to one of 'red', 'green' or 'blue'!\")\n", " aok = False\n", "\n", "# Make sure pick is set to a door.\n", "try:\n", " if pick not in doors:\n", " print(\"Error: please set the pick variable to one of 'red', 'green' or 'blue'!\")\n", " aok = False\n", "except NameError:\n", " print(\"Error: please set the pick variable to one of 'red', 'green' or 'blue'!\")\n", " aok = False\n", "\n", "# If there are no problems, print a statement to continue.\n", "if aok:\n", " print(\"Quick Check 2: everything is running okay, please continue.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### Plot twist\n", "\n", "***\n", "\n", "\n", "\n", "The car has now been randomly placed behind one of the doors and you have selected the door you think it might be behind.\n", "Next, the host throws in a twist.\n", "They open one of the doors that you have not picked and shows you there is a goat behind it.\n", "\n", "Note there is definitely a goat behind at least one of those two doors, and possibly behind both if you have already picked the car.\n", "The producers know where the car is and they make sure the host opens a door with a goat behind it.\n", "\n", "Run the cell below to simulate the host opening one of the doors.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Pick a random door that was neither picked by the contestant nor has the car behind it.\n", "show = random.choice([door for door in doors if door != car and door != pick])\n", "\n", "# Figure out which door was not opened or picked.\n", "notopen = [door for door in doors if door != pick and door != show][0]\n", "\n", "# What the host says.\n", "print(\"\\nHost:\")\n", "print(f\"\"\"\n", "You chose the {pick} door. The producers have told me I can reveal to you that there is a goat behind the {show} door.\n", "My question to you now is this: do you want to stick with the {pick} door or do you want to switch to the {notopen} door?\n", "\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### Your second choice\n", "\n", "***\n", "\n", "So, the host asks if you want to stick with your current door or change to the remaining door that you have not picked and they have not opened.\n", "Once you make your decision, all doors will be opened to reveal which one has the car behind it.\n", "\n", "In the next cell, indicate whether you will stay with your current choice of door or switch by setting the `stay` variable to `True` or `False`.\n", "\n", "- Set `stay` to `True` if you would like to stick with your current choice of door.\n", "- Set `stay` to `False` if you would like to switch to the other door offered by the host.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# You want to stay with your current door: True or False?\n", "stay = " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### Quick check 3\n", "\n", "***\n", "\n", "Run the cell below to make sure everything at this point is running okay.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Assume everything is running okay.\n", "aok = True\n", "\n", "# Make sure the stay variable is set to True or False.\n", "try:\n", " if stay not in [True, False]:\n", " print(\"Error: please set the stay variable to either True or False!\")\n", " aok = False\n", "except NameError:\n", " print(\"Error: please set the stay variable to either True or False!\")\n", " aok = False\n", "\n", "# If there are no problems, print a statement to continue.\n", "if aok:\n", " print(\"Quick Check 3: everything is running okay, please continue.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "### The reveal\n", "\n", "***\n", "\n", "Run the cell below to see if you would have won the car.\n", "Remember it's just a bit of fun so it doesn't matter whether or not you won.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Tell the user which door was opened by the host.\n", "print()\n", "print(\"Host:\")\n", "print(f\"\"\"\n", "You originally picked the {pick} door and I showed you there was a goat behind the {show} door.\n", "I asked you if you wanted to stay with the {pick} door or change to the {notopen} door.\n", "You decided to {'stay' if stay else 'change'}.\n", "I can now reveal to you that the car was behind the {car} door.\n", "You have {'won' if car == [pick, notopen][not stay] else 'not won'}.\n", "\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "\n", "### What to submit to the course page\n", "\n", "***\n", "\n", "Please run the following Python code and copy and paste the output below into the following form:\n", "\n", "[Click here for the form.](https://forms.office.com/r/JK6TJFw5wP)\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Please copy and paste the following output into the textbox on form:\")\n", "print()\n", "print(f\"{einstein},{monty},{savant},{car},{pick},{stay}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## End" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }