mardi 29 juin 2021

Monty Hall game coded in python, likelihood of winning after switching doors

There are probably similar threads already here, but anyways: First I created a working Monty Hall game in python:

You have 3 doors, pick one. If you find a goat (I wrote "poo"), you'll simply lose the game. If you don't choose the poodoor, you will be represented with a choice to change the door. It is argued that you have 2/3 chance of winning, if you choose to change. There is even a vsauce video about this..

So after the game worked, I created random inputs to the code and made the whole thing into a for loop, which adds to list the results: whether you chose to change or not, and if you won or not.

Regardless how how many times I run the loop, I get exactly 50/50 result with minor changes. Did I do my code wrong, or is the argument of 2/3 chance wrong?

If the subject interests you and/or if you know the answer to this, please comment! :) Comments on the code itself welcome too!

Thank you!

-Deep Man

Code1

Code2

Code3

Code4

EDIT: CONSOLE: Console

"""Monty hall testing deep_man"""

import random

def win_or_lose(choose, moneylist, choiselist):
    #automated change or not
    changeornot_autom = 0
    wonorlost = 0
    if choiselist [choose] == moneylist [choose]:
        # IF user chooses a poopdoor
        wonorlost = 0
        changevar = 0

    else:
        #IF user does not choose a poopdoor
        #make choiselist 1 become 2, now we search for money
        for loop in range(0, 3):
            if choiselist [loop] == 1:
                choiselist [loop] += 1
                changevar = 0
                break
            else:
                continue
        # IF user chooses the winning door:
        if choiselist [choose] == moneylist [choose]:
            for loop in range(0, 3):
                if moneylist [loop] == 1:
                    door = loop + 1
                    print(f"There was poop behind door: {door},"
                          f" Do you want to change to the unknown door? "
                          f"0 = no, 1 = yes: ")
                    changevar = random.randint(0, 1)
                else:
                    continue
            if changevar == 1:
                wonorlost = 1
            elif changevar == 0:
                wonorlost = 2
            else:
                wonorlost = -1
        #IF user chooses empty door
        else:
            for loop in range(0, 3):
                if moneylist [loop] == 1:
                    door = loop + 1
                    print(f"There was poop behind door: {door},"
                          f" Do you want to change to the unknown door?"
                          f" 0 = no, 1 = yes: ")
                    changevar = random.randint(0, 1)
                else:
                    continue
            if changevar == 1:
                wonorlost = 2
            elif changevar == 0:
                wonorlost = 1
            else:
                wonorlost = -1

    #Create a list of results
    wonorlost_and_changevar = [0, 0]
    wonorlost_and_changevar [0] = wonorlost
    wonorlost_and_changevar [1] = changevar
    return wonorlost_and_changevar


def moneyorpoop(moneylist):
    #What door has money, and what door has poo
    if moneylist == [0, 0, 2]:
        random_number = get_random_number(1)
        if random_number == 0:
            moneylist[random_number] += 1
        else:
            moneylist[random_number] += 1
    elif moneylist == [0, 2, 0]:
        random_number = get_random_number(1)
        if random_number == 0:
            moneylist[random_number] += 1
        else:
            moneylist[2] += 1
    else:
        random_number = get_random_number(1)
        if random_number == 0:
            moneylist[1] += 1
        else:
            moneylist[2] += 1
    return moneylist

def get_random_number(moneyorpoop):
    #Get the random number
    result = random.randint(0, moneyorpoop)
    return result

def main():
    switched_lost_won = [0, 0, 0]
    notswitched_lost_won = [0, 0, 0]

    # print instructions
    print("You have 3 doors. One door has poop behind it, one has money" \
          " behind it, third door has nothing behind it.")
    print("After your choise, the door which has poop behind it will be" \
          " revealed, and you will be presented")
    print("with a choise to change your choise to the remainind door")
    print("")

    for programloop in range(0, 1001):
        print("loop", programloop)
        doorlist = [0, 1, 2]
        #moneylist "2" means money, "1" means poop
        moneylist = [0, 0, 0]
        #choiselist "1" will be user choise
        choiselist = [0, 0, 0]
        choose = 0
        chosenvar = 0

        while chosenvar == 0:
            print("Choose a door: 1, 2, or 3: ")
            choose = random.randint(1, 3)
            if choose <= 3 and choose > 0:
                print("You chose", choose)
                choose -= 1
                choiselist [choose] += 1
                chosenvar += 1
            else:
                #Remnant of input based code
                print("You need to input a number between 1 and 3.")

        random_number = get_random_number(2)
        #add money behind the random door
        moneylist[random_number] += 2
        #add poop behind random door
        moneylist = moneyorpoop(moneylist)

        wonorlost_and_changevar = win_or_lose(choose, moneylist, choiselist)

        #wonorlost 2 = won, 1 = lost, 0 = poop, -1 = error
        #extract variables from list
        wonorlost = wonorlost_and_changevar [0]
        changevar = wonorlost_and_changevar [1]

        #print for debugging reasons
        print(changevar, wonorlost)

        #win or lose prints
        if wonorlost == -1:
            print("ERROR! Choose between 0 and 1 next time!")
        elif wonorlost == 0:
            print("You found poop. YOU LOSE!")
        elif wonorlost == 1:
            print("YOU LOSE!")
        else:
            print("YOU WIN!")

        #switched and lost won list
        if changevar == 1:
            switched_lost_won [0] += 1
            if wonorlost == 1:
                switched_lost_won[1] += 1
            elif wonorlost == 2:
                switched_lost_won[2] += 1
        #NOTswitched and lost won list
        if changevar == 0 and wonorlost != 0:
            notswitched_lost_won[0] += 1
            if wonorlost == 1:
                notswitched_lost_won[1] += 1
            elif wonorlost == 2:
                notswitched_lost_won[2] += 1

        print("Switched; Lost; Won")
        print(switched_lost_won)
        print("NOT switched; Lost; Won")
        print(notswitched_lost_won)
        print("")
        print("-------------------------------------------------------------")


    #After the programloop
    print("")
    print("Final Switched, lost, won", switched_lost_won)
    print("Final NOT switched, lost, won", notswitched_lost_won)

    print("Likelihood of winning IF SWITCHED:")
    likelyhood = switched_lost_won [2] / switched_lost_won [0]
    print(f"{likelyhood:.2f}")


if __name__ == "__main__":
    main()



Aucun commentaire:

Enregistrer un commentaire