python语言学习笔记(三)-----模拟投掷三个骰子,对游戏结果进行统计

#模拟投掷三个骰子,对游戏结果进行统计
import random
def roll_dice():
    '''模拟掷骰子'''
    roll_dot = random.randint(1, 6)
    return roll_dot
def main():
    dot_list=[0]*16
    key=list(range(3,19))  #[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
    dot_dict=dict(zip(key,dot_list)) #{3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0}
    total_times=10000  #掷骰子次数
    for i in range(total_times):
        roll_dot1=roll_dice()
        roll_dot2 = roll_dice()
        roll_dot3 = roll_dice()
        for j in range(3,19):
            if roll_dot1+roll_dot2+roll_dot3==j:
                dot_dict[j]+=1
    for k,v in dot_dict.items():
        print('掷三次骰子,得到的点数{},在{}次中出现了{}次'.format(k,total_times,v))

if __name__ == '__main__':
    main()

你可能感兴趣的:(python,python)