【问题标题】:Monty Hall Problem not working as intended蒙蒂霍尔问题没有按预期工作
【发布时间】:2021-09-03 04:22:44
【问题描述】:
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 视频来改进此代码,解决该问题的解决方案是什么?

【问题讨论】:

  • 在我看来这个问题更适合在Code Review Forum 中提出。 Code Review 是一个针对同行程序员代码审查的问答网站。在发布您的问题之前,请阅读有关如何在本网站上正确提问的相关指南。
  • 您有时得到错误答案的原因是您没有处理其中两种情况,特别是removed_choice==1 and user_choice==3 的任一值switch_not。您可以通过在最终的 elif 之后添加 else: print(removed_choice, user_choice, switch_not) 来查看这一点。
  • 哦,谢谢。从来不知道。干杯!
  • 我知道我错过了一个案例,谢谢,我的大脑今天没有工作。

标签: python-3.x random automation


【解决方案1】:

感谢@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多