解题如下:
from random import randintclass Die(): def __init__(self,sides=6): self.sides = sides def roll_die(self): x = randint(1, self.sides) return xnew_Die6 = Die()for i in range(10): print("[六面骰子]:"+ str(new_Die6.roll_die()))new_Die10 = Die(10)for i in range(10): print("[十面骰子]:"+ str(new_Die10.roll_die()))new_Die20 = Die(20)for i in range(10): print("[二十面骰子]:"+ str(new_Die20.roll_die())) >>> [六面骰子]:1 [六面骰子]:2 [六面骰子]:1 [六面骰子]:6 [六面骰子]:1 [六面骰子]:1 [六面骰子]:6 [六面骰子]:5 [六面骰子]:6 [六面骰子]:1 [十面骰子]:6 [十面骰子]:8 [十面骰子]:6 [十面骰子]:2 [十面骰子]:6 [十面骰子]:6 [十面骰子]:2 [十面骰子]:7 [十面骰子]:7 [十面骰子]:6 [二十面骰子]:5 [二十面骰子]:13 [二十面骰子]:6 [二十面骰子]:7 [二十面骰子]:19 [二十面骰子]:1 [二十面骰子]:7 [二十面骰子]:12 [二十面骰子]:2 [二十面骰子]:1