Question

J'ai écrit une fonction dans while loop, mais je ne sais pas comment l'arrêter. Quand il ne répond pas à sa condition finale, la boucle juste aller pour toujours. Comment puis-je arrêter?

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
Était-ce utile?

La solution

indentez simplement votre code correctement:

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

Vous devez comprendre que la déclaration dans votre exemple break va sortir de la boucle infinie que vous avez créé avec while True. Ainsi, lorsque la condition de rupture est vrai, le programme quitte la boucle infinie et de continuer à le prochain paragraphe en retrait. Comme il n'y a pas de bloc suivant dans votre code, la fonction se termine et ne reviennent pas quoi que ce soit. J'ai donc fixé votre code en remplaçant la déclaration par une déclaration return <=>.

Suite à votre idée d'utiliser une boucle infinie, c'est la meilleure façon de l'écrire:

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

Autres conseils

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

L'opérateur en Python is ne fait probablement pas ce que vous attendez. Au lieu de cela:

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

Je voudrais écrire comme ceci:

    if numpy.array_equal(tmp,universe_array):
        break

Les tests de l'opérateur <=> identité objet, ce qui est bien différent de l'égalité.

Je le ferais en utilisant une boucle comme indiqué ci-dessous:

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top