Reputation: 1
I'm new to programming with Python. Currently, I'm working on a program/algorithm to determine maintenance (combined replacement of multiple items) based on these items their condition. To be precise, I want to replace these items when one item's condition is below a predetermined threshold (for example 10%). The problem I have with my code, see below, is when this threshold is met all items are replaced.
def maintenance_action(self):
do_repair = False
for i in item:
if i.condition[-1] <= 10:
do_repair = True
break
if do_repair:
for i in items:
i.repair()
However, I want to include an additional threshold (let's say 50%) which excludes all items with a condition > 50% from the maintenance action. It is important that the first threshold is met (because this item must be replaced) before the second one 'kicks in' (the items I want to include). I hope someone can help me.
Thanks!
Upvotes: 0
Views: 65
Reputation: 33351
The simplest way would be to call repair right away, and not use a flag at all:
for i in items:
if i.condition[-1] <= 10:
i.repair()
Or, if you can't do that, you could build a list of items to be repaired in the first loop, and then process that list later:
items_to_repair = []
for i in item:
if i.condition[-1] <= 10:
items_to_repair.append(i)
# other code here
for i in items_to_repair:
i.repair()
Upvotes: 1
Reputation: 1342
If do_repair
is set to True
in the for
loop when the condition is met, all the variables are repaired in the second loop. In order to prevent this, you should repair the items which are met to the condition in the first loop. So I think there is no need to use the do_repair
variable and the second for
loop in this case.
def maintenance_action(self):
for i in item:
if i.condition[-1] <= 10:
i.repair()
Upvotes: 0