只需正确缩进代码即可:
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
您需要了解,
break示例中的语句将退出您使用创建的无限循环
whileTrue。因此,当中断条件为True时,程序将退出无限循环并继续执行下一个缩进块。由于代码中没有以下代码块,因此该函数结束并且不返回任何内容。因此,我通过将
break语句替换为语句来修复了您的代码
return。
按照您的想法使用无限循环,这是编写它的最佳方法:
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



