You need to reset a to True for each now value. Also a == False is just not a (as if a==True is if a)for n in range(2,13): a = True for i in range(2,n): if n % i ==0: a = False if not a: print(n, ": Not prime.") else: print(n,": Prime")关于循环的一些建议Some advices about the loop在数字的平方根处停止使用 break 在知道错误后停止循环使用for/else,如果没有发现中断,则进入elsestop at the square root of the numberuse a break to stop looping after knowing it's falseuse the for/else, if no break has been found it goes into the elsefor n in range(2,13): for i in range(2,int(n**0.5)+1): if n % i ==0: print(n, ": Not prime.") break else: print(n,": Prime")2 : Prime3 : Prime4 : Not prime.5 : Prime6 : Not prime.7 : Prime8 : Not prime.9 : Not prime.10 : Not prime.11 : Prime12 : Not prime. 这篇关于问质数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 10:12