Posts: 39
Threads: 14
Joined: Oct 2018
OrgH = [73.35,111.37,92.70,70.73,67.71,77.84]
BtmGap = 32
MinRH = 12
Div = 7
tH = 400
sH = sum(OrgH)
q = Div - len(OrgH)
def Optm(S, H, GH, q):
s = H + GH
L = [S,GH]
if S >= s:
K = S - s
if S < s and GH > MinRH / 12:
K = Optm(S, H, GH - 1, q)
if S < s and GH <= MinRH / 12:
K = Optm(S + 1, H, GH, q)
if q < 0:
print("Somethign wrong with the category!")
if q == 0:
return 0
else:
minGap = K / q
if minGap < MinRH:
print("b")
return Optm(S + 1, H, GH, q)
else:
print("a")
print(S)
print(GH)
print(minGap)
return minGap In the script above, my goal is to make the last "else" executes once and get only one result. But, now, it generates several iterations.
Can someone help? Thank you in advance!
Posts: 4,220
Threads: 97
Joined: Sep 2016
It's not clear, because you haven't shown us how you call Optm in the first place. But if you only want the else clause to execute if none of the if clauses do, then the middle if's all need to be elif's (short for else if).
Posts: 39
Threads: 14
Joined: Oct 2018
(Mar-13-2019, 07:11 PM)ichabod801 Wrote: It's not clear, because you haven't shown us how you call Optm in the first place. But if you only want the else clause to execute if none of the if clauses do, then the middle if's all need to be elif's (short for else if).
OrgH = [73.35,111.37,92.70,70.73,67.71,77.84]
BtmGap = 32
MinRH = 12
Div = 7
tH = 400
sH = sum(OrgH)
q = Div - len(OrgH)
def Optm(S, H, GH, q):
s = H + GH
if S >= s:
K = S - s
if S < s and GH > MinRH / 12:
K = Optm(S, H, GH - 1, q)
if S < s and GH <= MinRH / 12:
K = Optm(S + 1, H, GH, q)
if S == 494:
print(K)
print(K/q)
print(s)
print(S-s)
print(GH)
if q < 0:
print("Somethign wrong with the category!")
if q == 0:
return 0
else:
minGap = K / q
if minGap < MinRH:
return Optm(S + 1, H, GH, q)
if minGap >= MinRH and S <= s:
return Optm(5 + 2, H, GH, q)
else:
return minGap
L = Optm(tH, sH, BtmGap, q) sorry, I missed one line.
Posts: 4,220
Threads: 97
Joined: Sep 2016
I don't see an obvious solution, and your code is totally opaque (no documentation, non-descriptive variable names). The only thing I note at this point is that line 40 should maybe be return Optm(S + 2, H, GH, q) instead of Optm(5 + 2, H, GH, q) (that is, ess not five).
|