{"id":83,"date":"2016-11-10T22:02:30","date_gmt":"2016-11-10T22:02:30","guid":{"rendered":"http:\/\/codeclub.garethgwyther.co.uk\/?p=83"},"modified":"2016-11-10T22:02:30","modified_gmt":"2016-11-10T22:02:30","slug":"microbit-monty-hall-problem","status":"publish","type":"post","link":"https:\/\/codeclub.garethgwyther.co.uk\/?p=83","title":{"rendered":"MicroBit Monty Hall problem"},"content":{"rendered":"<p>Today we are going to construct a small game using the micro-bit which using an idea below.<\/p>\n<p>https:\/\/en.wikipedia.org\/wiki\/Monty_Hall_problem<\/p>\n<p>Suppose you&#8217;re on a game show, and you&#8217;re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No.\u00a01, and the host, who knows what&#8217;s behind the doors, opens another door, say No.\u00a03, which has a goat. He then says to you, &#8220;Do you want to pick door No.\u00a02?&#8221; Is it to your advantage to switch your choice?<\/p>\n<p>Vos Savant&#8217;s response was that the contestant should switch to the other door (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Monty_Hall_problem#CITEREFvos_Savant1990a\">vos Savant 1990a<\/a>). Under the standard assumptions, contestants who switch have a <span class=\"sfrac nowrap\">2<span class=\"visualhide\">\/<\/span>3<\/span> chance of winning the car, while contestants who stick to their initial choice have only a <span class=\"sfrac nowrap\">1<span class=\"visualhide\">\/<\/span>3<\/span> chance.<\/p>\n<p>So we are going to create a game based on the above theory to prove it right or wrong.<\/p>\n<p><strong>List<\/strong> Prizes<br \/>\n<strong>Strings<\/strong><br \/>\nGoat<br \/>\nCar<br \/>\nGoat<\/p>\n<ol>\n<li>Go to this page\u00a0<a href=\"\/?page_id=61\" target=\"_blank\" rel=\"noopener\">Click here to start<\/a><\/li>\n<li>Create a list in python and print position 0 in the list.\n<pre class='code'>\nprizes = [\"Goat\",\"Goat\",\"Car\"]\nprint(prizes[0])\n<\/pre>\n<\/li>\n<li>This return the same thing over and over again.<\/li>\n<li>We need to randomise the list.<\/li>\n<li>Place this code above prizes\n<pre class='code'>from random import shuffle\nfrom random import randint<\/pre>\n<\/li>\n<li>Then before the print and after prizes and in this line\n<pre class='code'>shuffle(prizes)<\/pre>\n<\/li>\n<li>Run the code this should now be randomised you should not see car appearing every so often<\/li>\n<li>Code should now look like this.\n<pre class='code'>\nfrom random import shuffle\nfrom random import randint\nprizes = [\"Goat\",\"Goat\",\"Car\"]\nshuffle(prizes)\nprint(prizes[0])\n<\/pre>\n<\/li>\n<li>We now need to ask a question remove the rint(prizes[0])and replace with below.\n<pre class=\"code\">\nprint(\"Monty: Please select a door between 0 and 2\")\ndoor = input()\nprint(\"Monty: You Selected door number \"+door)\n<\/pre>\n<\/li>\n<li>Code should now look like this.\n<pre class='code'>\nfrom random import shuffle\nfrom random import randint\nprizes = [\"Goat\",\"Goat\",\"Car\"]\nshuffle(prizes)\nprint(\"Monty: Please select a door between 0 and 2\")\ndoor = input()\nprint(\"Monty: You Selected door number \"+door)\n<\/pre>\n<\/li>\n<li>Due to input() being not forced as int we need to convert it an integer add this after<br \/>\nfrom random import randint<\/p>\n<pre class=\"code\">\ndef convertStr(s):\n    try:\n        ret = int(s)\n    except ValueError:\n        ret = float(s)\n    return ret\n\n<\/pre>\n<\/li>\n<li>\nYou&#8217;re code should now look like this.<\/p>\n<pre class='code'>\nfrom random import shuffle\nfrom random import randint\n\ndef convertStr(s):\n    try:\n        ret = int(s)\n    except ValueError:\n        ret = float(s)\n    return ret\n\nprizes = [\"Goat\",\"Goat\",\"Car\"]\nshuffle(prizes)\nprint(\"Monty: Please select a door between 0 and 2\")\ndoor = input()\nprint(\"Monty: You Selected door number \"+door)\n<\/pre>\n<\/li>\n<li>\ndef declares what is known as a function<br \/>\nfunctions can take parameters (variables input etc)<br \/>\nin this case we want the input of door<br \/>\nAdd this code after the print(&#8220;You Selected door number &#8220;+door)<\/p>\n<pre class=\"code\">\nselected_door_int = convertStr(door)\n<\/pre>\n<\/li>\n<li>\nOur presenter Monty needs to select a door add this code after selected_door_int = convertStr(door)<\/p>\n<pre class=\"code\">\nprint(\"MONTY: I will now select a door\")\npresenter_door = randint(0,2)\nwhile 1:\n    if presenter_door != selected_door_int or prizes[presenter_door] != \"Car\":\n    \tpresenter_door = randint(0,2)\n    break\nprint(\"MONTY: I selected door number \"+str(presenter_door)+\" and my prize was a \"+prizes[presenter_door]);\n<\/pre>\n<\/li>\n<li>\nYou&#8217;re code should now look like this.<\/p>\n<pre class='code'>\nfrom random import shuffle\nfrom random import randint\n\ndef convertStr(s):\n    try:\n        ret = int(s)\n    except ValueError:\n        ret = float(s)\n    return ret\n\nprizes = [\"Goat\",\"Goat\",\"Car\"]\nshuffle(prizes)\nprint(\"MONTY: Please select a door between 0 and 2\")\ndoor = input()\nprint(\"You Selected door number \"+door)\nselected_door_int = convertStr(door)\nprint(\"MONTY:I will now select a door\")\npresenter_door = randint(0,2)\nwhile 1:\n    if presenter_door == selected_door_int or prizes[presenter_door] == \"Car\":\n    \tpresenter_door = randint(0,2)\n    break\nprint(\"I selected door number \"+str(presenter_door)+\" and my prize was a \"+prizes[presenter_door]);\n<\/pre>\n<\/li>\n<li>presenter_door = randint(0,2) this means select a random number between 0 and 1 (0,1,2) this is all the indexs of our list<\/li>\n<li> While 1: means star the loop<\/li>\n<li> if presenter_door == selected_door_int or prizes[presenter_door] == &#8220;Car&#8221;: this means if either conidtion is met (presenter door equals door person has selected or prize of selected index equals car remember Monty is not allowed to select the car)<\/li>\n<li>Now for the final element should the person take a risk or not according to the statistical nature of above they should<\/li>\n<li>Add this code after print(&#8220;I selected door number &#8220;+str(presenter_door)+&#8221; and my prize was a &#8220;+prizes[presenter_door]);\n<pre class=\"code\">\nprint(\"MONTY: Do you want to change you're mind on you're door type Yes Or No?\")\nanswer = input()\nprint(\"MONTY: You selected \"+answer)\nif answer.lower() == \"no\":\n    print(\"You have won a \"+prizes[selected_door_int])\nelse:\n\tfinal_door = randint(0,2)\n        while 1:\n            if final_door == selected_door_int or presenter_door == final_door:\n                final_door = randint(0,2)\n            break\n\tprint(\"You have won a \"+prizes[final_door])\n<\/pre>\n<\/li>\n<li>Code should now look like this.\n<pre class='code'>\nfrom random import shuffle\nfrom random import randint\n\ndef convertStr(s):\n    try:\n        ret = int(s)\n    except ValueError:\n        ret = float(s)\n    return ret\n\nprizes = [\"Goat\",\"Goat\",\"Car\"]\nshuffle(prizes)\nprint(\"MONTY: Please select a door between 0 and 2\")\ndoor = input()\nprint(\"You Selected door number \"+door)\nselected_door_int = convertStr(door)\nprint(\"MONTY:I will now select a door\")\npresenter_door = randint(0,2)\nwhile 1:\n    if presenter_door == selected_door_int or prizes[presenter_door] == \"Car\":\n    \tpresenter_door = randint(0,2)\n    break\nprint(\"MONTY: I selected door number \"+str(presenter_door)+\" and my prize was a \"+prizes[presenter_door]);\nprint(\"MONTY: Do you want to change you're mind on you're door type Yes Or No?\")\nanswer = input()\nprint(\"MONTY: You selected \"+answer)\nif answer.lower() == \"no\":\n    print(\"You have won a \"+prizes[selected_door_int])\nelse:\n\tfinal_door = randint(0,2)\n        while 1:\n            if final_door == selected_door_int or presenter_door == final_door:\n                final_door = randint(0,2)\n            break\n\tprint(\"You have won a \"+prizes[final_door])\n<\/pre>\n<\/li>\n<li>We now need to convert this to microbit code go <a href=\"https:\/\/www.microbit.co.uk\/app\/#edit:ea47e84d-de7d-4fd4-c51d-96b00713715e\" target=\"_blank\" rel=\"noopener\">here <\/a><\/li>\n<li>We are presented with the code as follows\n<pre class=\"code\">\n# Add your Python code here. E.g.\nfrom microbit import *\n\n\nwhile True:\n    display.scroll('Hello, World!')\n    display.show(Image.HEART)\n    sleep(2000)\n\n<\/pre>\n<\/li>\n<li>\nLets starting adding our code<\/p>\n<pre class=\"code\">\n# Add your Python code here. E.g.\nfrom microbit import *\nimport random\n\n\nprizes = [\"Goat\",\"Goat\",\"Car\"]\ndoor = 0\nwhile True:\n    display.scroll('MONTY: Please select a door')\n    display.clear()\n    if button_a.get_presses() or button_b.get_presses():\n        door = random.randint(0,2)\n        display.clear()\n        text = \"Monty: You Selected door number \"+str(door)\n        display.scroll(text)\n        display.clear()\n        display.scroll(\"Monty: I will now select a door\")\n     \tpresenter_door = random.randint(0,2)\n        while 1:\n            if presenter_door == door or prizes[presenter_door] == \"Car\":\n                presenter_door = random.randint(0,2)\n            break\n        display.scroll(\"MONTY: I selected door number \"+str(presenter_door)+\" and my prize was a \"+prizes[presenter_door])\n        display.clear()\n        display.scroll(\"MONTY: Do you want to change you're mind on you're door type Yes(a) Or No(b)?\")\n     \tif button_a.get_presses():\n            final_door = random.randint(0,2)\n            while 1:\n                if final_door == door or presenter_door == final_door:\n                    final_door = random.randint(0,2)\n                break\n            display.scroll(\"You have won a \"+prizes[final_door])\n        elif button_b.get_presses(): \n            display.scroll(\"You have won a \"+prizes[door])\n<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Today we are going to construct a small game using the micro-bit which using an idea below. https:\/\/en.wikipedia.org\/wiki\/Monty_Hall_problem Suppose you&#8217;re on a game show, and you&#8217;re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No.\u00a01, and the host, who knows what&#8217;s behind [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-83","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=83"}],"version-history":[{"count":0,"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions"}],"wp:attachment":[{"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeclub.garethgwyther.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}