幸运"7"游戏,用计算机模拟掷骰子的过程,测算两个骰子点数之和为7的概率。 (用while循环架构)
from random import *
count = 0
a = 0
while count <= 100000:
num1 = randint(1,6)
num2 = randint(1,6)
if num1 + num2 == 7:
count += 1
a += 1
else:
count += 1
print("输入[]次后,两个骰子点数之和为7的概率为[]".format(count-1,a/count))
输出结果:输入100000次后,两个骰子点数之和为7的概率为0.16641833581664184