第一种方法 from random import randint def roll(n=3): total = 0 for x in range(n): total += randint(1,6) return total print(roll()) 第二种方法 import random for i in range(3): num = 0 for j in range(3): num += random.randint(1, 6) print(num) 运行结果