sfyhip 发表于 2018-8-9 06:40:30

三门问题之python解答

#!/usr/bin/python  
#coding:utf-8
  
total=100000
  
a=b=c=d=e=f=0.00
  
x=0
  
from random import randint
  
list=["sheep1","sheep2","car"]
  
tuple=("yes","no")
  
while x!=total:
  
   t=randint(0,2)
  
   i=randint(0,1)
  
#   print list,tuple
  
   if list=="car":
  
      if tuple=="yes":
  
         a+=1
  
      else:
  
         b+=1
  
   elif list=="sheep1":
  
if tuple=="yes":
  
         c+=1
  
      else:
  
         d+=1
  
   elif list=="sheep2":
  
      if tuple=="yes":
  
         e+=1
  
      else:
  
         f+=1
  
   x+=1
  
print "总次数为%d"%(total)
  
print "换%d" %(a+c+e),"不换%d" %(b+d+f)
  
print "不换赢的概率为%.2f%%"%(b/(c+b+e)*100)
  
print "换赢的概率为%.2f%%"%((c+e)/(c+b+e)*100)
页: [1]
查看完整版本: 三门问题之python解答