思路:
1.导入模块random中的randint()。
2.建立类Die,设置属性sides,默认为6
3.建立方法roll_die(),用循环使其运行10次,用randint输出随机数(范围为骰子面)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #创作者:光树 from random import randint class Die: def __init__(self, sides='6'): self.sides = sides def roll_die(self): i = 0 if i <= 10: for n in range(11): print(randint(1, eval(self.sides)), end=" ") i += 1 die = Die() die.roll_die() die = Die('10') die.roll_die() die = Die('20') die.roll_die() |