开关
Python练习(六)
给一个数,判断它是否是素数(质数):
除了1和它自身外,不能被小于它的正整数整除的就是素数。
简化点就是:能整除,不是素数
不能整除,是素数
可以参考另一篇《Python练习(三)》中有介绍如何计算出1-100之内的所有素数
- num = int(input(‘please enter a number: >>> ‘))
- on_off = 0 ‘‘‘开关‘‘‘
- for i in range(2,num):
- if num%i == 0:
- on_off = 1
- break
- else:
- on_off = 0
- if on_off == 1:
- print(‘可以整除,开关设为1,不是素数‘)
- else:
- print(‘不可以整除,开关赋为0,是素数‘)
输出结果:
- please enter a number: >>> 11
- 不可以整除,开关赋为0,是素数
- please enter a number: >>> 4
- 可以整除,开关设为1,不是素数
- please enter a number: >>> 8
- 可以整除,开关设为1,不是素数
- please enter a number: >>> 97
- 不可以整除,开关赋为0,是素数
Python练习(六)
来源: http://www.bubuko.com/infodetail-2316036.html