import random
options = ["goat", "goat", "car"]
def monty_hall_stick(number_of_goes):
games_played = 0
wins = 0
while games_played < number_of_goes:
random.shuffle(options)
guess = random.choice(options)
if guess == "car":
wins = wins + 1
games_played = games_played + 1
decimal = wins / number_of_goes
percentage = decimal * 100
percentage = percentage // 1
print("The win percentage of sticking with your first choice is", percentage, "%")
def monty_hall_switch(number_of_goes):
games_played = 0
wins = 0
while games_played < number_of_goes:
options = ["goat", "goat", "car"]
random.shuffle(options)
guess = random.choice(options)
guess = options.index(guess)
del options[guess]
options.remove("goat")
guess = random.choice(options)
if guess == "car":
wins = wins + 1
games_played = games_played + 1
decimal = wins / number_of_goes
percentage = decimal * 100
percentage = percentage // 1
print("The win percentage of swapping after revealed goat is", percentage, "%")
print("Welcome to the Monty Hall problem simulator")
method = input("Would you like to use the Switch method (Sw) or the Stick method (St)?: ")
method = method.lower
number_of_goes = int(input("How many games should the simulator run?: "))
if method == "sw" or "switch":
monty_hall_switch(number_of_goes)
elif method == "st" or "stick":
monty_hall_stick(number_of_goes)
else:
print("Thats not a valid method. Try again")
Because of this line:
See:
Another thing to be aware of:
This…
method = method.lower
sound be…
method = method.lower()
Better still…
method = input("Would you like to use the Switch method (Sw) or the Stick method (St)?: ").lower()
Also (not that it makes too much of a difference here) when you have code such as if method == "sw" or "switch":
personally, I’d use if method in ("sw", "switch")
That way it’s an easy code edit to add more options to the tuple should more options be needed.
1 Like