Pregunta

Me escribió una while loop en una función, pero no sé cómo detenerlo. Cuando no se ajusta a su estado final, el bucle sólo tiene que ir para siempre. ¿Cómo puedo detenerlo?

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
¿Fue útil?

Solución

acaba de sangrar el código correctamente:

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

Es necesario comprender que el break comunicado en su ejemplo saldrá del bucle infinito que ha creado con while True. Así que cuando la condición es verdadera ruptura, el programa sale automáticamente del bucle infinito y continuar con el siguiente bloque sangrado. Puesto que no hay bloque siguiente en su código, la función termina y no devuelve nada. Así que me he fijado su código mediante la sustitución de la return declaración de un comunicado <=>.

Tras su idea de utilizar un bucle infinito, esta es la mejor manera de escribir que:

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

Otros consejos

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

El is operador en Python, probablemente, no hace lo que usted espera. En lugar de esto:

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

Me gustaría escribir así:

    if numpy.array_equal(tmp,universe_array):
        break

Las pruebas <=> objeto operador identidad, que es algo muy diferente de la igualdad.

lo haría utilizando un bucle como se muestra a continuación:

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top