我只是想制作一个生成骰子的代码(在python中).这是代码:
import random
class Dice:
def _init_(self, number_dice):
self._dice = [6] * number_dice
def roll_dice(self):
for d in range(len(self._dice)):
self._dice[d] = random.randit(1, 6)
self._dice.sort()
def print_roll(self):
length = len(self._dice)
print(str(lenth) + "dice:" + str(self._dice))
my_dice = Dice(2)
my_dice.roll_dice()
my_dice.print_roll()
Run Code Online (Sandbox Code Playgroud)
编译器对第18行说了些什么.我是编程的新手,所以任何事情都有帮助=]
iCo*_*dez 11
你需要前后两个下划线__init__:
def __init__(self, number_dice):
self._dice= [6] *number_dice
Run Code Online (Sandbox Code Playgroud)
否则,Python将该方法视为自定义方法而不是特殊__init__构造方法.
| 归档时间: |
|
| 查看次数: |
8645 次 |
| 最近记录: |