# Python入门coding之一：2去了哪里？

XAMPP下载 533浏览

1. >>> for n in range(2, 10):
2. … for x in range(2, n):
3. … if n % x == 0:
4. … print n, ‘equals’, x, ‘*’, n/x
5. … break
6. … else:
7. … # loop fell through without finding a factor
8. … print n, ‘is a prime number’

1. 2 is a prime number
2. 3 is a prime number
3. 4 equals 2 * 2
4. 5 is a prime number
5. 6 equals 2 * 3
6. 7 is a prime number
7. 8 equals 2 * 4
8. 9 equals 3 * 3

1. #!/usr/bin/env python3.3
2. #–encoding-utf8–
3. #2~10以内的素数
4. for n in range(2,10):
5. for x in range(2,n):
6. if n % x == 0:
7. print(n,’equals’,x,’*’,n/x)
8. break
9. else:
10. #loop fell through without finding a factory
11. print(n,’is a prime number’)
12. break

1. 3 is a prime number
2. 4 equals 2 * 2.0
3. 5 is a prime number
4. 6 equals 2 * 3.0
5. 7 is a prime number
6. 8 equals 2 * 4.0
7. 9 is a prime number

1、为何原书里else这段suite里没有break？（python3.3里如果没有break，素数时会输出多次）

2、在后面的代码输出里，素数2去了哪里？

1. # test range function
2. for i in range(2,10):
3. print(i)

1. 2
2. 3
3. 4
4. 5
5. 6
6. 7
7. 8
8. 9
9.