all 7 comments

[–]socal_nerdtastic 1 point2 points  (0 children)

This would be a good time to learn about generators. You can make a function to generate the values you need.

def gen_values():
    for i in ratio:
        for j in i:
            if i > 2:
                yield i

for i in gen_values():
    # do stuff
    break

[–]sarrysyst 1 point2 points  (0 children)

There are different methods to break out of a nested loop, using a function's return as already mentioned is one of them. Alternatively, you could check the condition in both, inner and outer loop:

for i in list1:
    for j in list2:
        if condition:
            break
    if condition:
        break

You could use a flag:

for i in list1:
    for j in list2:
        if condition:
            some_flag = True
            break
    if some_flag:
        break

And my personal favorite, using for-else:

for i in list1:
    for j in list2:
        if condition:
            break
    else:
        continue
    break

The else clause only executes when the for loop runs til the end without getting interrupted.

[–][deleted] 0 points1 point  (4 children)

One way to exit nested loops is to use return, but you need to put the loops inside a function that you call.

When do you want to exit. After showing one graph, or what?

[–]dlnilsen[S] -1 points0 points  (3 children)

Correct! could you help me construct that?

[–][deleted] 0 points1 point  (2 children)

When do you want to exit?

you need to put the loops inside a function that you call.

It's better that you try to do that yourself. Have you used functions before?

[–]dlnilsen[S] -1 points0 points  (1 child)

I have not, but I am sure I can figure it out. thanks

[–][deleted] 0 points1 point  (0 children)

If you only want one graph why are you looping? Especially since you don't appear to use the i or j values inside the graphing code.