编写一个ball.py文件,代码如下
#!/usr/bin/env python
# coding: utf-8
__author__ = 'www.py3study.com'
import random
class selectball(object):
def __init__(self):
self.run()
def run(self):
while True:
numstr = input('输入测试的次数:')
try:
num = int(numstr)
except ValueError:
print(u'要求输入一个整数!')
continue
else:
break
ball = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for i in range(num):
n = random.randint(1,10)
ball[n - 1] += 1
for i in range(1, 11):
print(u'获取第{}号球的概率为:{}'.format(i, ball[i-1]*1.0/num))
if __name__ == '__main__':
SB = selectball()
应该看到的效果
选取的次数越多,这个趋势就越明显,理想状态下,所有球的选取几率是一样的