简体   繁体   English

for循环不接受联合值

[英]for loop not accepting fed values

I'm working on a program that simulates fish in a closed system. 我正在开发一个在封闭系统中模拟鱼的程序。 I want to be able to create multiple instances of each fish so that each can be tracked individually kind of like pokemon. 我希望能够为每个鱼创建多个实例,以便可以像神奇宝贝一样单独跟踪每个实例。

I have made this section for my init values: 我已经针对我的init值做了本节:

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=[]

and

my update section while TilapiaWeight < 0.5 and Waste >= 0: 我的更新部分,而TilapiaWeight <0.5和Waste> = 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

What I can't understand is how I can run this for say Tilapia 1 and Tilapia 2 where they might have different starting weights. 我不明白的是我该如何运行罗非鱼1和罗非鱼2,它们可能具有不同的起始权重。 I was also trying to use 我也在尝试使用

TilapiaLog.append(set((PrintTilapiaList)))

at the end of the second block to log all the values as it iterates but it just makes a longer and longer list of the same set repeated x times... 在第二个块的末尾记录所有的值,但它只是重复遍历了x次而得到的相同集合的列表越来越长...

I then updated according to below suggestions to: 然后,我根据以下建议进行了更新:

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

Then you create many fish instances: 然后创建许多鱼实例:

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

You group them in a collection 您将它们分组在一个集合中

fishes = (tilapia1, tilapia2)

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

then you make them live, evolve, whatever you like 然后你让它们活着,发展,随你喜欢

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)

This returns: 返回:

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

How can this be if I am giving it these values? 如果我给它们这些值怎么办?

You create a class Tilapia hold the characteristics and behavior of your fish. 您创建了一个Tilapia类,以容纳鱼类的特征和行为。

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

Then you create many fish instances: 然后创建许多鱼实例:

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

You group them in a collection 您将它们分组在一个集合中

tilapias = [tilapia1, tilapia2, ...]

then you make them live, evolve, whatever you like 然后你让它们活着,发展,随你喜欢

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

You probably need to find a good tutorial on OOP 您可能需要找到有关OOP的良好教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM