Dice rolling dice calculated sum (the number of dice can be changed)

from random import randint

"""
    摇骰子
    
    :param n: 色子的个数

    :return: n颗色子点数之和
"""


def roll_dice(n=2):
    total = 0
    # 此处的下划线是一个占位符,不关心具体的元素内容,就是让他循环这么多次
    for _ in range(n):
        total += randint(1, 6)
        print(total)
    return total


# 调用方法roll_dice默认值摇2个骰子
print('两个骰子%s' % roll_dice())
# 也可以自己传参
print('三个骰子%s' % roll_dice(3))

 

Published 52 original articles · won praise 34 · views 2627

Guess you like

Origin blog.csdn.net/weixin_38114487/article/details/103780679