for循环不接受馈送值

时间:2017-09-13 09:40:57

标签: python python-3.x

我正在研究一个在封闭系统中模拟鱼类的程序。 我希望能够创建每条鱼的多个实例,以便每个鱼都能像口袋妖怪一样被跟踪。

我已将此部分用于我的初始值:

TilapiaWeight = 0.05
TilapiaFCR = 0.1
TilapiaFeedStatus = 10
TilapiaFeedAmount = 0.9*TilapiaWeight
WasteAmt = (1-TilapiaFCR)*TilapiaFeedAmount
Waste = 10
days = ["Mon"],["Tue"],["Wed"],["Thu"],["Fri"],["Sat"],["Sun"]
time = 0
PlantUptakeList = [0.002,0.003] #the uptake rate of each plant
Plants = []#Testing list appending["Green Lettuce","Red Lettuce"]
PlantUptakeSum = 0
PlantsPerM2 = 30
PTX=[]

我的更新部分     而罗非鱼则是< 0.5和废物> = 0:

TilapiaFeedStatus = TilapiaFeedStatus + TilapiaFeedAmount
TilapiaWeight = TilapiaWeight + (TilapiaFCR*TilapiaFeedAmount)
Waste = Waste + WasteAmt
TilapiaFeedStatus=TilapiaFeedStatus - TilapiaFeedAmount
Waste = Waste - PlantUptakeSum
PlantsUpdate = [x+0.0001 for x in PlantUptakeList]
PlantUptakeList = PlantsUpdate
PlantUptakeSum = sum(PlantUptakeList)
PrintTilapia = "Weight",TilapiaWeight,"Waste",Waste,"Day:",time,"Plant uptake",PlantUptakeList
PrintTilapiaList = [TilapiaFeedStatus, TilapiaFeedAmount, TilapiaWeight, Waste, time]
time = time + 1

我能理解的是,如果罗非鱼1和罗非鱼2可能有不同的起始重量,我可以这样做。 我也试图使用

TilapiaLog.append(set((PrintTilapiaList)))

在第二个块的末尾,在迭代时记录所有值,但它只是重复x次的相同集合的更长和更长的列表......

然后根据以下建议更新:

class Fish:
def __init__(self, type, number, weight, status, feed, fcr, wastestatus): 
    self.type = type
    self.number = number
    self.weight = weight
    self.status = status
    self.feed = feed
    self.fcr = fcr
    self.wastestatus = wastestatus
def update(self, type, number, weight, status, feed, fcr, wastestatus):
    status = status + feed
    weight = weight + (fcr * feed)
    wastestatus = wastestatus + (1-fcr)
    PrintTilapia = type, number, weight, status, feed, fcr, wastestatus
    print(type, number, weight, status, feed, fcr, wastestatus)

    return type, number, weight, status, feed, fcr, wastestatus
    #...do something to this fish

然后创建许多鱼类实例:

tilapia1 = 'Tilapia',1,0.005,1,1,0.15,0
tilapia2 = 'Tilapia',2,0.01,1,1,0.15,0

您将它们分组在一个集合

fishes = (tilapia1, tilapia2)

print(tilapia1) #('Tilapia', 1, 0.005, 1, 1, 0.15, 0)

然后你让它们活着,进化,无论你喜欢什么

time_span = 10
 for t in range(time_span):
  for f in fishes:
    print(f) #('Tilapia', 1, 0.005, 1, 1, 0.15, 0)
    Fish.update(f)

返回:

    Fish.update(tilapia1)
    TypeError: update() missing 7 required positional arguments: 'type', 'number', 'weight', 'status', 'feed', 'fcr', and 'wastestatus'

如果我给它这些值,怎么会这样?

1 个答案:

答案 0 :(得分:1)

您创建了一个类Tilapia来保存鱼的特征和行为。

class Tilapia:
    def __init__(self, weight, status, ...):
        self weight = weight
        self.status = status
    def update(self):
        ...do something to this fish 

然后你创建了许多鱼类实例:

tilapia1 = Tilapia(12, 'happy')
tilapia2 = Tilapia(5, 'hungry')
...

您将它们分组在一个集合

tilapias = [tilapia1, tilapia2, ...]

然后你让它们活着,进化,无论你喜欢什么

for t in range(time_span):
    for tilapia in tilapias:
        tilapia.update()

您可能需要在OOP上找到一个好的教程