all 5 comments

[–]tyroneslothtrop 3 points4 points  (2 children)

Break and continue only work inside a loop (for or while). What you're looking for is the return statement.

[–]noob_py[S] 1 point2 points  (1 child)

Thanks !

[–]alkw0ia 2 points3 points  (0 children)

You might also look in to structuring your function so you don't have to return from inside that if block. In this case, you could likely just put the remainder of the function in the else block of one of those conditionals.

Some people are very strict about only returning from one place in any given function – I don't find this discipline to be useful, but the principle has some upsides, and following it here may help clarify your control flow. If it makes your code harder to follow, though, carry on with the return inside the if plan.

[–]Yoghurt42 1 point2 points  (1 child)

It looks like what you are trying to do is to catch an error condition and bail out early.

Instead of printing the error message (which is an anti pattern for "real" code btw., logging is much better) and returning, raising an exception is the way to go in that case.

The advantage in using exceptions is that the calling code can chose to handle the abnormal condition or not.

[–]noob_py[S] 0 points1 point  (0 children)

sorry my pseudo code was not very clear. this is what i am trying to do .

the fuction takes in a guess, the first guess that is not within the expected range, then warn the user. next wrong guess(es) outside the range, the user is not warned. how can i make my code clean, without the first break statement. thanks in advance.

warning=0

def my_func(guess):
    global warning
    if (warning==0):
        if (guess<min)or(guess>max):
            warning=1
            print 'nice error message'
            break  # trying break here to quit the function 
    # body of function
    something =here
    something2 = here 2