Monty Hall问题未按预期工作


import random
total_winnings = 0
total_changed_wins = 0
total_losses = 0
def monthall(repeat):
global total_winnings, total_losses, total_changed_wins
for repeating in range(repeat):
winning_choice = random.randint(1, 3)
user_choice = random.randint(1, 3)
if winning_choice == 1 and user_choice == 1:
removed_choice = random.randint(2, 3)
elif winning_choice == 2 and user_choice == 1:
removed_choice = 3
elif winning_choice == 3 and user_choice == 1:
removed_choice = 2
elif winning_choice == 1 and user_choice == 2:
removed_choice = 3
elif winning_choice == 2 and user_choice == 2:
removed_choice = random.randrange(1, 4, 2)
elif winning_choice == 3 and user_choice == 2:
removed_choice = 1

elif winning_choice == 1 and user_choice == 3:
removed_choice = 2
elif winning_choice == 2 and user_choice == 3:
removed_choice = 1
elif winning_choice == 3 and user_choice == 3:
removed_choice = random.randint(1, 2)

switch_not = random.randint(1, 2)
if removed_choice == 1 and user_choice == 2 and switch_not == 1:
if user_choice == winning_choice:
total_winnings += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 1 and user_choice == 2 and switch_not == 2:
user_choice = 3
if user_choice == winning_choice:
total_winnings += 1
total_changed_wins += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 2 and user_choice == 1 and switch_not == 1:
if user_choice == winning_choice:
total_winnings += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 2 and user_choice == 1 and switch_not == 2:
user_choice = 3
if user_choice == winning_choice:
total_winnings += 1
total_changed_wins += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 3 and user_choice == 1 and switch_not == 1:
if user_choice == winning_choice:
total_winnings += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 3 and user_choice == 1 and switch_not == 2:
user_choice = 2
if user_choice == winning_choice:
total_winnings += 1
total_changed_wins += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 3 and user_choice == 2 and switch_not == 1:
if user_choice == winning_choice:
total_winnings += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 3 and user_choice == 2 and switch_not == 2:
user_choice = 1
if user_choice == winning_choice:
total_winnings += 1
total_changed_wins += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 2 and user_choice == 3 and switch_not == 1:
if user_choice == winning_choice:
total_winnings += 1
elif user_choice != winning_choice:
total_losses += 1
elif removed_choice == 2 and user_choice == 3 and switch_not == 2:
user_choice = 1
if user_choice == winning_choice:
total_winnings += 1
total_changed_wins += 1
elif user_choice != winning_choice:
total_losses += 1 

monthall(10)
total_unchanged_wins = total_winnings - total_changed_wins
total_winnings = "{:,}".format(total_winnings)
total_losses = "{:,}".format(total_losses)
total_changed_wins = "{:,}".format(total_changed_wins)
total_unchanged_wins = "{:,}".format(total_unchanged_wins)

print(f"Total winnings: {total_winnings} ,nChanged Doors Winnings: {total_changed_wins} ,nUnchaged Doors winnings {total_unchanged_wins} ,nTotal losses: {total_losses}")

是的,我知道这是你可能见过的最糟糕的代码,但请注意,这是我的第一个大项目,我不知道如何迭代,我试着在youtube上搜索它,看看其他人的代码,从来都不明白它们的意思(因为你不应该这样做,不专业,毫无意义(,所以我走了很长的路。

我面临的唯一问题是,总奖金+总损失加起来不等于重复的次数(是的,我知道,这是我的错(。有没有什么方法可以通过观看YT视频来改进这个代码,该视频显示了这样的自动化,解决这个问题的解决方案是什么?

感谢@JonSG发现了一个丢失的案例:

elif removed_choice == 1 and user_choice == 3 and switch_not == 1:
if user_choice == winning_choice:
total_winnings += 1
elif user_choice != winning_choice:
total_losses += 1

elif removed_choice == 1 and user_choice == 3 and switch_not == 2:
user_choice = 2
if user_choice == winning_choice:
total_winnings += 1
total_changed_wins += 1
elif user_choice != winning_choice:
total_losses += 1

最新更新