该代码应查找给定范围内的所有素数。但是这里有些错误
import math
def display(num, truth):
if truth:
print(num)
lower = int(input("Enter lower limit :"))
upper = int(input("Enter upper limit :"))
for x in range(lower, upper, 1):
b = 2
c = True
a = math.sqrt(x)
while b < a:
if x%b != 0:
continue
else:
c = False
break
display(x,c)
print("Done")
我希望它应该输出,例如2到6:
Enter lower limit :2
Enter upper limit :6
2
3
5
Done
但是输出是(对于相同范围)
Enter lower limit :2
Enter upper limit :6
2
3
4
请注意,“完成”不会出现 当我尝试关闭外壳程序时,Python警告该程序仍在运行。
答案 0 :(得分:0)
我不确定您所编写的某些代码。
怎么样呢?
import math
lower = int(input("Enter lower limit :"))
upper = int(input("Enter upper limit :"))
for x in range(lower,upper + 1):
# prime numbers are greater than 1
if x > 1:
for i in range(2,x):
if (x % i) == 0:
break
else:
print(x)
print("Done")