标签:rand not ros 抽屉 dia random 选择 microsoft input
import random
def no_change(times):
glass_ball = 0
diamond = 0
times2=times
while times>0:
drawers={‘drawer1‘: False, ‘drawer2‘: False, ‘drawer3‘: False}
drawer_with_diamond=random.randint(1,3) #获取随机数1-3
drawers[‘drawer%d‘%drawer_with_diamond]=True #随机在3个抽屉中放入钻石
your_choice=random.randint(1,3) #再次获取随机数1-3
what_you_get=drawers[‘drawer%d‘%your_choice]
if what_you_get==False: #判定选到的是什么
glass_ball+=1
else:
diamond+=1
times-=1
winning_rate=diamond/(diamond+glass_ball) #获取赢得钻石的概率
print(‘一共试验了%d次,你获得钻石的概率为%f‘%(times2, winning_rate))
def change_drawer(times):
glass_ball = 0
diamond = 0
times2=times
while times>0:
drawers = {‘drawer1‘: False, ‘drawer2‘: False, ‘drawer3‘: False}
drawer_with_diamond = random.randint(1, 3) # 获取随机数1-3
drawers[‘drawer%d‘ % drawer_with_diamond] = True # 随机在3个抽屉中放入钻石
your_choice = random.randint(1, 3) # 再次获取随机数1-3
your_drawer=‘drawer‘+str(your_choice)
for things in drawers:
if drawers[things] == True: #获取钻石在第几个抽屉中
if things==your_drawer: #如果此时你选的,正是有钻石的那个抽屉,并且你换抽屉,那么你必然拿到玻璃球
glass_ball+=1
if things!=your_drawer: #如果此时你选的,是有玻璃球的,主持人给你展示另一个有玻璃球的的,你换抽屉,必然拿到钻石
diamond+=1
times-=1
winning_rate=diamond/(diamond+glass_ball) #获取赢得钻石的概率
print(‘一共试验了%d次,你获得钻石的概率为%f‘ % (times2, winning_rate))
def main():
change_or_not=str(input(‘你选择换还是不换抽屉?换/不换:‘))
if change_or_not==‘换‘:
times=int(input(‘请输入你想试验的次数:‘))
change_drawer(times)
else:
times = int(input(‘请输入你想试验的次数:‘))
no_change(times)
if __name__==‘__main__‘:
main()
2020-05-27
标签:rand not ros 抽屉 dia random 选择 microsoft input
原文地址:https://www.cnblogs.com/hany-postq473111315/p/12971307.html