python应用-n颗骰子的和出现的次数

   

from random import randint
def roll_dice(n):
    total=0
    for _ in range (n):
        num=randint(1,6)
        total+=num
    return total
    #有了列表容器我们可以使用一个变量来保存多个数据
    #更为重要的是我们可以使用循环对列表中保存的数据进行操作
def main():
    f=[0]*11
    for _ in range (60000):
        face=roll_dice(2)
        f[face-2]+=1
    for index in range (len(f)):
        print('%d摇出了%d次'% (index+2,f[index]))
    #直接通过对for_in循环对容器进行便利
    for counter in f:
        print(counter)
if __name__ == '__main__':
    main()

  结果: 2摇出了1687次 3摇出了3391次 4摇出了5040次 5摇出了6668次 6摇出了8329次 7摇出了10095次 8摇出了8325次 9摇出了6603次 10摇出了4986次 11摇出了3239次 12摇出了1637次 1687 3391 5040 6668 8329 10095 8325 6603 4986 3239 1637

原文地址:https://www.cnblogs.com/68xi/p/8546458.html