python - 调试 Google 面试问题模拟 - '3 门天堂模拟'

python - debugging a Google interview question simulation - '3 doors to heaven simulation'

作为业余 Python 用户,我正在使用 Python 代码来模拟经典的科技公司面试问题:

You go to Heaven and see 3 gates. 
One leads straight to Heaven. 
One sends you to Hell for 1 day, then sends you back to the gates. 
One sends you to Hell for 2 days, then sends you back to the gates. 
Every time you return to the gates, they will have been shuffled, so you can't tell from past experience which is which. They could even be standing in the same place. 
On average, how long would you expect to be tortured by hellfire before you meet God?

我写了一个 Python 代码来模拟这个:

trials = [0]
totalDays = [0]
days = [0]
import random 

def chooseGates():

    choice = random.randint(1,3)
    if choice == 1:
        days[0] += 1
        print("You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.")
        chooseGates()
    elif choice == 2:
        days[0] += 2
        print("You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.")
        chooseGates()
    else:
        print("You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.")
    trials[0] += 1

    totalDays[0] += days[0]
    print("You've done ", trials[0], "trials and this time you've spent", days[0], "days in Hell. ")
    days[0] = 0
    print(" ")
    print("The game starts again.")
    
    return True
    

for i in range (0,10):
    chooseGates()
print("A total of " , trials[0], "trials run. ")
print("The average number of days needed was ", totalDays[0]/trials[0])

麻烦的是,这就是 Programmiz Python Online Compiler 给我的:

You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done  1 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done  2 trials and this time you've spent 2 days in Hell. 
 
The game starts again.
You've done  3 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done  4 trials and this time you've spent 7 days in Hell. 
 
The game starts again.
You've done  5 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You've done  6 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You've done  7 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You've done  8 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You've done  9 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done  10 trials and this time you've spent 0 days in Hell. 
 
...
 
The game starts again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.
You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.
You've done  20 trials and this time you've spent 8 days in Hell. 
 
The game starts again.
You've done  21 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You've done  22 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You've done  23 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You've done  24 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
You've done  25 trials and this time you've spent 0 days in Hell. 
 
The game starts again.
A total of  25 trials run. 
The average number of days needed was  0.88
> 

我进行如此少的试验的原因是我经常感觉到一个错误导致结果为 0。监控变量后,我发现代码似乎跳过了 if 迭代:

if choice == 1:
        days[0] += 1
        print("You chose door 1 and spent 1 day in Hell. You return to the gates, which have been shuffled, and choose again.")
        chooseGates()
    elif choice == 2:
        days[0] += 2
        print("You chose door 2 and spent 2 days in Hell. You return to the gates, which have been shuffled, and choose again.")
        chooseGates()
    else:
        print("You chose door 3, which is the right door. Well done, thou good and faithful servant - enter thou into the joy of our Lord.")

例如,这里有一些结果:

You've done  5 trials and this time you've spent 0 days in Hell. 

The game starts again.
You've done  6 trials and this time you've spent 0 days in Hell. 

The game starts again.
You've done  7 trials and this time you've spent 0 days in Hell. 

The game starts again.
You've done  8 trials and this time you've spent 0 days in Hell. 

The game starts again.
You've done  9 trials and this time you've spent 0 days in Hell. 

Python 如何跳过每次都需要打印内容的整个 if 迭代? (里面有 else 语句,它必须打印一些东西!)当我只要求 10 次时,它是如何进行 32 次试验的?有人可以帮我吗?

这段代码怎么样?

import random
import numpy as np


def HeavenOrNot():
    days=0
    while True:
        choice=random.choice([0,1,2])
        if choice==0:
            break
        elif choice==1:
            days+=1
        elif choice==2:
            days+=2
    return days

print('Expected number of days in hell:',np.mean([HeavenOrNot() for i in range(100000)]))