代码:
1 def is_prime(number): 2 if number in(1,2): 3 return Ture 4 for idx in range(2,number): 5 if number%idx==0: 6 return False 7 return True 8 9 10 11 12 def print_primes(begin,end): 13 for number in range(begin,end+1): 14 if is_prime(number): 15 print(f"{number } is a prime") 16 17 18 19 20 begin=11 21 end=25 22 print_primes(begin,end)
效果:
1 11 is a prime 2 13 is a prime 3 17 is a prime 4 19 is a prime 5 23 is a prime
总结:
- 先写问题需求:打印区间素数 begin=11, end=25, print_primes(begin,end);
- 再写素数打印;
- 最后写判断素数;
- 这里用到True,False,首字母必须大写。