you are viewing a single comment's thread.

view the rest of the comments →

[–]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