python中用while和if判断质数

#从控制台输入一个数,判断是否为质数
a=int(input("输入一个数:"))
b=2
e=0
while b<=a:
c=a%b
if c==0:
e += 1
b+=1
if e>1:
print("不是质数")
else:
print("是质数")

前一个输入1会出错

#从控制台输入一个数,判断是否为质数
a=int(input("输入一个数:"))
b=1
e=0
while b<=a :
c=a%b
if c==0:
e += 1
b+=1
if e==2:
print("是质数")
else:
print("不是质数")
posted @ 2019-02-23 01:01  飞飞阿  阅读(4014)  评论(0编辑  收藏  举报