本文主要是介绍python 学习 羊车门问题 DAY19,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
(一)
import random
counts = 10000
sum_y1 = 0
sum_y2 = 0
ls1 = ["羊1","羊2","车"]
ls2 = ["羊","车"]
for i in range(counts): #第一种
pc = random.choice(ls1)
cc = random.choice(ls1)
if pc == cc:
sum_y1 += 1
for j in range(counts): #第二种
pc = random.choice(ls2)
cc = random.choice(ls2)
if pc == cc:
sum_y2 += 1
print("第一种概率为{0:.2f} , 第二种概率为{1:.2f}".format(sum_y1/counts,sum_y2/counts))
(二)
import random
counts = 10000
sum_y1 = 0
sum_y2 = 0
for i in range(counts): #第一种
pc = random.randint(0,2)
cc = random.randint(0,2)
if pc == cc:
sum_y1 += 1
for j in range(counts): #第二种
pc = random.randint(0,1)
cc = random.randint(0,1)
if pc == cc:
sum_y2 += 1
print("第一种概率为{0:.2f} , 第二种概率为{1:.2f}".format(sum_y1/counts,sum_y2/counts))
这篇关于python 学习 羊车门问题 DAY19的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!