[PYTHON] Monty Hall Game

#Monty Hall Analysis
#Kevin Wong - kmw396
#CS164-069 / Lecture B
import random # we need to initalize the python in-built random library

# this will ask the user to enter a seed for testing purposes. It has a try & except catch in case the user enters a non-interger. It will gracefully exit the program if it's a non-integer.
seed = input("Enter Random Seed:\n")
try:
    seed = int(seed)
    random.seed(seed)
except ValueError:
    print(seed, "is not a number!")
    exit()
    
print("Welcome to Monty Hall Analysis")
print("Enter \'exit\' to quit.")

# This is the input for the number of times the Monty Hall Analysis test should run. It has a try & except catch in case the user enters an non-interger. It also allows the user to exit the program.
numberTest = input("How many tests should we run?\n")
if numberTest == 'exit':
    exit()
valid = False
while not valid:
        try:
            numberTest = int(numberTest)
            valid = True
        except ValueError:
            numberTest = input("Please enter a number:\n")
            if numberTest == 'exit':
                exit()

# These variables are designed to hold the values of stsy and switch if you're testing more than 10 times. 
stayRatio = 0
switchRatio = 0

# Initalize list for Doors.
Door = ['G', 'C', 'G']

while numberTest != 'exit': #checks if the user enters exit
        
    if numberTest <= 10: # runs the program with extra output if the number of tests is less than 10
        for i in range (1, numberTest + 1): 
            random.shuffle(Door) # shuffles the list of doors
            playerSelect = random.randint(0,2) # player picks door using random
            montySelect = random.randint(0,2) # monty picks door using random 
            # monty won't pick a door if the player picked it already or if the door is 'C'
            while montySelect == playerSelect or montySelect == Door.index('C'): 
                montySelect = random.randint(0,2)
            # this will tell the print function to either print "not switch" or "switch" based on if the player has a Car or a Goat. This will add a 1 to the respective variables if the player is correct or incorrect
            if Door[playerSelect] == 'C':
                switch = 'not switch'
                stayRatio = stayRatio + 1
            if Door[playerSelect] == 'G':
                switch = 'switch'
                switchRatio = switchRatio + 1
            # since python's random numbers start at 0, we need to add 1 to it.
            playerSelect += 1
            montySelect += 1
            print("Game %d" % (i)) #prints "Game 'X'"
            print("Doors:",Door) #prints shuffled doors
            print("Player Selects: Door",playerSelect) #prints the door the player selected
            print("Monty Selects: Door",montySelect) #prints the door monty selected
            print("Player should",switch,"to win.") #tells you if you should switch or not switch
            stayFinal = (stayRatio / numberTest * 100) # calculates the ratio of stay
            switchFinal = (switchRatio / numberTest * 100) # calculates the ratio of switching 
        print("Stay Won","%.1f%%" % stayFinal,"of the time.") #prints final stayratio
        print("Switch Won","%.1f%%" % switchFinal,"of the time.") #prints final switchratio

    if numberTest > 10: # runs the program without extra output if the number of tests is greater than 10
        for i in range(1, numberTest + 1): 
            random.shuffle(Door) # shuffles the list of doors
            playerSelect = random.randint(0,2) # player picks door using random 
            montySelect = random.randint(0,2) # monty picks door using random 
            # monty won't pick a door if the player picked it already or if the door is 'C'
            while montySelect == playerSelect or montySelect == Door.index('C'):
                montySelect = random.randint(0,2)
            # this will add a 1 to the respective variables if the player is correct or incorrect
            if Door[playerSelect] == 'C':
                stayRatio = stayRatio + 1
            if Door[playerSelect] == 'G':
                switchRatio = switchRatio + 1
            stayFinal = (stayRatio / numberTest * 100) # calculates the ratio of stay
            switchFinal = (switchRatio / numberTest * 100) # calculates the ratio of switching 
        print("Stay Won","%.1f%%" % stayFinal,"of the time.") #prints final stayratio
        print("Switch Won","%.1f%%" % switchFinal,"of the time.") #prints final switchratio
    break
    if numberTest == 'exit':
        exit()