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’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?
Vos Savant’s response was that the contestant should switch to the other door (vos Savant 1990a). Under the standard assumptions, contestants who switch have a 2/3 chance of winning the car, while contestants who stick to their initial choice have only a 1/3 chance.
So we are going to create a game based on the above theory to prove it right or wrong.
List Prizes
Strings
Goat
Car
Goat
- Go to this page Click here to start
- Create a list in python and print position 0 in the list.
prizes = ["Goat","Goat","Car"] print(prizes[0])
- This return the same thing over and over again.
- We need to randomise the list.
- Place this code above prizes
from random import shuffle from random import randint
- Then before the print and after prizes and in this line
shuffle(prizes)
- Run the code this should now be randomised you should not see car appearing every so often
- Code should now look like this.
from random import shuffle from random import randint prizes = ["Goat","Goat","Car"] shuffle(prizes) print(prizes[0])
- We now need to ask a question remove the rint(prizes[0])and replace with below.
print("Monty: Please select a door between 0 and 2") door = input() print("Monty: You Selected door number "+door) - Code should now look like this.
from random import shuffle from random import randint prizes = ["Goat","Goat","Car"] shuffle(prizes) print("Monty: Please select a door between 0 and 2") door = input() print("Monty: You Selected door number "+door) - Due to input() being not forced as int we need to convert it an integer add this after
from random import randintdef convertStr(s): try: ret = int(s) except ValueError: ret = float(s) return ret -
You’re code should now look like this.
from random import shuffle from random import randint def convertStr(s): try: ret = int(s) except ValueError: ret = float(s) return ret prizes = ["Goat","Goat","Car"] shuffle(prizes) print("Monty: Please select a door between 0 and 2") door = input() print("Monty: You Selected door number "+door) -
def declares what is known as a function
functions can take parameters (variables input etc)
in this case we want the input of door
Add this code after the print(“You Selected door number “+door)selected_door_int = convertStr(door)
-
Our presenter Monty needs to select a door add this code after selected_door_int = convertStr(door)
print("MONTY: I will now select a door") presenter_door = randint(0,2) while 1: if presenter_door != selected_door_int or prizes[presenter_door] != "Car": presenter_door = randint(0,2) break print("MONTY: I selected door number "+str(presenter_door)+" and my prize was a "+prizes[presenter_door]); -
You’re code should now look like this.
from random import shuffle from random import randint def convertStr(s): try: ret = int(s) except ValueError: ret = float(s) return ret prizes = ["Goat","Goat","Car"] shuffle(prizes) print("MONTY: Please select a door between 0 and 2") door = input() print("You Selected door number "+door) selected_door_int = convertStr(door) print("MONTY:I will now select a door") presenter_door = randint(0,2) while 1: if presenter_door == selected_door_int or prizes[presenter_door] == "Car": presenter_door = randint(0,2) break print("I selected door number "+str(presenter_door)+" and my prize was a "+prizes[presenter_door]); - 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
- While 1: means star the loop
- if presenter_door == selected_door_int or prizes[presenter_door] == “Car”: 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)
- Now for the final element should the person take a risk or not according to the statistical nature of above they should
- Add this code after print(“I selected door number “+str(presenter_door)+” and my prize was a “+prizes[presenter_door]);
print("MONTY: Do you want to change you're mind on you're door type Yes Or No?") answer = input() print("MONTY: You selected "+answer) if answer.lower() == "no": print("You have won a "+prizes[selected_door_int]) else: final_door = randint(0,2) while 1: if final_door == selected_door_int or presenter_door == final_door: final_door = randint(0,2) break print("You have won a "+prizes[final_door]) - Code should now look like this.
from random import shuffle from random import randint def convertStr(s): try: ret = int(s) except ValueError: ret = float(s) return ret prizes = ["Goat","Goat","Car"] shuffle(prizes) print("MONTY: Please select a door between 0 and 2") door = input() print("You Selected door number "+door) selected_door_int = convertStr(door) print("MONTY:I will now select a door") presenter_door = randint(0,2) while 1: if presenter_door == selected_door_int or prizes[presenter_door] == "Car": presenter_door = randint(0,2) break print("MONTY: I selected door number "+str(presenter_door)+" and my prize was a "+prizes[presenter_door]); print("MONTY: Do you want to change you're mind on you're door type Yes Or No?") answer = input() print("MONTY: You selected "+answer) if answer.lower() == "no": print("You have won a "+prizes[selected_door_int]) else: final_door = randint(0,2) while 1: if final_door == selected_door_int or presenter_door == final_door: final_door = randint(0,2) break print("You have won a "+prizes[final_door]) - We now need to convert this to microbit code go here
- We are presented with the code as follows
# Add your Python code here. E.g. from microbit import * while True: display.scroll('Hello, World!') display.show(Image.HEART) sleep(2000) -
Lets starting adding our code
# Add your Python code here. E.g. from microbit import * import random prizes = ["Goat","Goat","Car"] door = 0 while True: display.scroll('MONTY: Please select a door') display.clear() if button_a.get_presses() or button_b.get_presses(): door = random.randint(0,2) display.clear() text = "Monty: You Selected door number "+str(door) display.scroll(text) display.clear() display.scroll("Monty: I will now select a door") presenter_door = random.randint(0,2) while 1: if presenter_door == door or prizes[presenter_door] == "Car": presenter_door = random.randint(0,2) break display.scroll("MONTY: I selected door number "+str(presenter_door)+" and my prize was a "+prizes[presenter_door]) display.clear() display.scroll("MONTY: Do you want to change you're mind on you're door type Yes(a) Or No(b)?") if button_a.get_presses(): final_door = random.randint(0,2) while 1: if final_door == door or presenter_door == final_door: final_door = random.randint(0,2) break display.scroll("You have won a "+prizes[final_door]) elif button_b.get_presses(): display.scroll("You have won a "+prizes[door])