是否可以让列表对象成为模块的实例并使用 "for i in range(dice)" 在该模块中执行函数?
Is it possible to have list objects be instances of a module and execute a function in that module using "for i in range(dice)"?
我想总结我在另一个模块中编写的骰子总数。与其手动更改每个实例的骰子数量,我只想设置一次并执行多少个骰子。所以我想将模块的实例存储在一个列表中,然后 运行 每次程序循环遍历列表时的 roll 函数。函数实例和模块实例是否用作列表对象?
from plotly.graph_objs import Bar, Layout
from plotly import offline
from die import Die
#Store the results in a list
results = []
#Set the variable names of the dice
die_1 = Die()
die_2 = Die()
die_3 = Die()
dice = [die_1, die_2, die_3]
dice_num = len(dice)
#Roll them like Vegas
for roll_num in range(1000):
for i in range(dice):
f = i in dice
roll_total = f.roll()
results.append(roll_total)
您似乎想制作多个骰子并在每个骰子上调用一个方法:
#Store the results in a list
results = []
#Set the variable names of the dice
# Suppose N is a number
dice = [Dice() for _ in range(N)]
#Roll them like Vegas
for roll_num in range(1000):
for d in dice:
roll_total = d.roll()
results.append(roll_total)
我想总结我在另一个模块中编写的骰子总数。与其手动更改每个实例的骰子数量,我只想设置一次并执行多少个骰子。所以我想将模块的实例存储在一个列表中,然后 运行 每次程序循环遍历列表时的 roll 函数。函数实例和模块实例是否用作列表对象?
from plotly.graph_objs import Bar, Layout
from plotly import offline
from die import Die
#Store the results in a list
results = []
#Set the variable names of the dice
die_1 = Die()
die_2 = Die()
die_3 = Die()
dice = [die_1, die_2, die_3]
dice_num = len(dice)
#Roll them like Vegas
for roll_num in range(1000):
for i in range(dice):
f = i in dice
roll_total = f.roll()
results.append(roll_total)
您似乎想制作多个骰子并在每个骰子上调用一个方法:
#Store the results in a list
results = []
#Set the variable names of the dice
# Suppose N is a number
dice = [Dice() for _ in range(N)]
#Roll them like Vegas
for roll_num in range(1000):
for d in dice:
roll_total = d.roll()
results.append(roll_total)