I am looking for an elegant way to write this code. (Python 2.7)
The code below sits in a for loop. When the starting status
of an item
is say, s2
, I need it to run iteratively until it reaches status
s7
.
If the success returns False
, then it will stop and stay at that level, and the loop continues with the next item
.
I'm hoping not to use try:
to avoid the use of exceptions. The True / False success
boolean should be sufficient for this purpose. Please correct me if you think I should consider otherwise.
Here's the code I have attempted. I am trying to find a more elegant solution.
def do_s1(): # similar for other status
if condition:
success = True
else:
success = False
return success
for i in items:
if status[i] = s1:
success_s1 = do_s1()
if success_s1:
success_s2 = do_s2()
if success_s2:
success_s3 = do_s3()
# all the way until do_s7(), iteratively
if status[i] = s2:
do_s2()
if success:
do_s3() # all the way until do_s7(), iteratively
...
if status[i] = s7:
do_s7()
# some code
The loop should iterate to the next item every time success turns False, or when it reaches s7
You can use the ternary operator in the do_s1 function:
The remaining code is a bit confusing since I don't know what type of variables status contains and what s1,s2 and s3 are.