Tuesday, March 1, 2011

How can i stop a While loop?

Hi,i wrote a while loop in a function,but dont know how to stop it...when it doesnt meet its final condition,the loop just go for ever....how can i stop it?thanks in advance.

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            break    #i want the loop to stop and return 0 if the 
                     #period is bigger than 12
        if period>12:  #i wrote this line to stop it..but seems it 
                       #doesnt work....help..
            return 0
        else:   
            return period
From stackoverflow
  • Doesn’t python have some kind of break statement?

    NONEenglisher : i have used break..but it still doesnt work.....
  • just indent your code correctly:

    def determine_period(universe_array):
        period=0
        tmp=universe_array
        while True:
            tmp=apply_rules(tmp)#aplly_rules is a another function
            period+=1
            if numpy.array_equal(tmp,universe_array) is True:
                return period
            if period>12:  #i wrote this line to stop it..but seems its doesnt work....help..
                return 0
            else:   
                return period
    

    You need to understand that the break statement in your example will exit the infinite loop you've created with while True. So when the break condition is True, the program will quit the infinite loop and continue to the next indented block. Since there is no following block in your code, the function ends and don't return anything. So I've fixed your code by replacing the break statement by a return statement.

    Following your idea to use an infinite loop, this is the best way to write it:

    def determine_period(universe_array):
        period=0
        tmp=universe_array
        while True:
            tmp=apply_rules(tmp)#aplly_rules is a another function
            period+=1
            if numpy.array_equal(tmp,universe_array) is True:
                break
            if period>12:  #i wrote this line to stop it..but seems its doesnt work....help..
                period = 0
                break
    
        return period
    
    NONEenglisher : i tried this as well.but it gives out wrong rusult...
    Mapad : yes, because you had another bug in your code: a break statement which was preventing your function to return something. I removed it now.
    NONEenglisher : ..still,it always return 1
    NONEenglisher : but it always return 0 now...thanks alot though~~~u r so nice
    NONEenglisher : GOT IT!!!HAHA!!SO HAPPY!!!THANK YOU VERY MUCH!!!!
    nosklo : don't check if something() is True: Check the object direct like in "if numpy.array_equal(tmp,universe_array):"
  • def determine_period(universe_array):
        period=0
        tmp=universe_array
        while period<12:
            tmp=apply_rules(tmp)#aplly_rules is a another function
            if numpy.array_equal(tmp,universe_array) is True:
                break 
            period+=1
    
        return period
    
    annakata : +1 but would be nice to point out what+why, "while true" eeek
    NONEenglisher : doesnt work....return wrong reslut..always return 13
    Daok : Joel code loop until period is 12 than stop looping and return period... it,s normal... Joel code isn't what you want
    Joel Coehoorn : This is closer. And yes, I do expect him to think for himself some as well.
    NONEenglisher : @ Joel Coehoorn ,Thanks alot:)
    Joel Coehoorn : Be careful: this still won't return what you want if the if condition isn't true. I'm leaving that last point as an exercise for the reader.
    NONEenglisher : @Joel Coehoorn,i cant stop myself to say you are really a good teacher!love this formu,because it has so many teachers like you.
  • I recommend learning about pdb, and stepping through your code to see exactly what is going on. This will make debugging this kind of errors much easier in the future.

    Learning how to use a debugger is one of the first skills a beginning programmer should develop.

    PDB

  • The is operator in Python probably doesn't do what you expect. Instead of this:

        if numpy.array_equal(tmp,universe_array) is True:
            break
    

    I would write it like this:

        if numpy.array_equal(tmp,universe_array):
            break
    

    The is operator tests object identity, which is something quite different from equality.

  • I would do it using a for loop as shown below :

    def determine_period(universe_array):
        tmp = universe_array
        for period in xrange(1, 13):
            tmp = apply_rules(tmp)
            if numpy.array_equal(tmp, universe_array):
                return period
        return 0
    

0 comments:

Post a Comment