Ask coding questions

← Back to all posts
help with a control flow tool
rogeryoung (0)

I have been trying very hard to get the following lines of code to be looped over with a
control flow statement. It seemed like the while loop would be the choice but perhaps there
there is another tool that is used and I am very sorry that I do not understand these control
flow tools better.
I want the program to simply stop if all of the conditions in the following lines are met
but to continue on to the next line if any of these conditions are false:

vtival < float(bucket +.35bucket) or vtival > float(bucket -.15bucket)
tltval < float(bucket +.35bucket) or tltval > float(bucket -.15bucket)
iauval < float(bucket +.35bucket) or iauval > float(bucket -.15bucket)
shyval < float(bucket +.35bucket) or shyval > float(bucket -.15bucket)

I am thanking anyone who helps me with this in advance to either tell me what to do
or what I should read to understand this more fully.

Comments
hotnewtop
xxpertHacker (860)

Just a tip, you might want to make the float(bucket + .35...) and float(bucket - .15...) into seperate variables for reuse and readability.

Otherwise this would just be a (kinda large) if statement:

if vtival < float(bucket +.35bucket)
  or vtival > float(bucket -.15bucket)
  or tltval < float(bucket +.35bucket)
  or tltval > float(bucket -.15bucket)
  or iauval < float(bucket +.35bucket)
  or iauval > float(bucket -.15bucket)
  or shyval < float(bucket +.35bucket)
  or shyval > float(bucket -.15bucket) {
    stop();
} else {
    // nothing
}
rogeryoung (0)

I am so very grateful that you took the time to help me and I am so sorry that I can not get your program to work --to flow - or any other thing that I have tried. I am sure you know that this is Python that I am using. Anything more that you could do to help would be profoundly appreciated. @xxpertHacker

xxpertHacker (860)

@rogeryoung The only difference would be syntax:

lower_bound = float(bucket + .35 * bucket)
upper_bound = float(bucket - .15 * bucket)
def is_within_range(n):
	return n < lower_bound or n > upper_bound

if is_within_range(vtival) or is_within_range(tltval) or is_within_range(iauval) or is_within_range(shyval):
    stop()

And no, I had no reason to assume that you were using Python.

rogeryoung (0)

This code with quit() instead of stop() stops the program whether it it is out of range or not -- stop() is not recognized.
lower_bound = float(bucket +.35bucket)
upper_bound = float(bucket -.15
bucket)
def is_within_range(n):
return n < lower_bound or n > upper_bound

if is_within_range(vtival) or is_within_range(tltval) or is_within_range(iauval) or is_within_range(shyval):
quit() @xxpertHacker

xxpertHacker (860)

@rogeryoung Then use quit, ¯\_(ツ)_/¯ how was I supposed to have known what you named your function?

Does it work when using quit in place of stop?

rogeryoung (0)

The problem with the various suggestions has been that the programs have proceeded to the next line if the condition was not met but the next line said to stop. I still have not mastered these control flow tools well. It seems like what I need is SOMETHING like: if (condition is met) break, else, continue. The general idea of this program is to rebalance a portfolio if any of its holdings vary from set percentages by certain amounts so it stops if those conditions are met and proceeds on to the rest of the program (the next line in it) if it needs rebalancing. Thank you for all of the help you have given me in the past. @xxpertHacker

xxpertHacker (860)

@rogeryoung I'd recommend posting the actual Repl, and making another post, so it would get more attention.

rogeryoung (0)

Do you mean posting the entire program I am working on? It has about 90 lines. Or do you mean more of the program where the problem is than I posted to you? Perhaps I need to really explain myself better. I so wish I could master this whole area of control flow tools so I would not have to bother others here. Thank you for your time!! @xxpertHacker

xxpertHacker (860)

@rogeryoung Move the entire program into a Repl.

It could be a thousand lines, or fifteen lines, it shouldn't matter.

SixBeeps (5060)

You could use an if-statement for that. Just stick those conditions in and stop the program if the condition is met:

if (vtival < float(...)):
  quit()

# Do more if the condition wasn't met
rogeryoung (0)

The problem with the various suggestions has been that the programs have proceeded to the next line if the condition was not met but the next line said to stop. I still have not mastered these control flow tools well. It seems like what I need is SOMETHING like: if (condition is met) break, else, continue. The general idea of this program is to rebalance a portfolio if any of its holdings vary from set percentages by certain amounts so it stops if those conditions are met and proceeds on to the rest of the program (the next line in it) if it needs rebalancing. Thank you for all of the help you have given me in the past. @SixBeeps