我正在做一个显示几个素数的程序,是使用函数所必需的。 例如,1 = 10 = 2,3,5,7
但在 - 打印i-的最后一行出现错误...我无法理解那是什么
def isPrime(num):
if num < 2:
return False
i = 2
for i in range(2,int(math.sqrt(num)+1)):
if (num % i == 0):
return False
return True
def main():
print ("this program do prime numbers")
start = int(raw_input("start number "))
finish = int(raw_input('finish number: '))
for i in range(start,finish):
if isPrime(i):
print i
答案 0 :(得分:1)
您的问题是您没有围绕i
括号中的print i
。 In Python 3, print
has been changed from a statement to a function.以下是您编辑的代码:
def isPrime(num):
if num < 2:
return False
i = 2
for i in range(2,int(math.sqrt(num)+1)):
if (num % i == 0):
return False
return True
def main():
print ("this program do prime numbers")
start = int(raw_input("start number "))
finish = int(raw_input('finish number: '))
for i in range(start,finish):
if isPrime(i):
print(i) #Previously "print i"
之前正在运行:
bash-3.2$ python3.4 prime.py
File "test.py", line 19
print i
^
SyntaxError: invalid syntax
bash-3.2$
编辑后:
bash-3.2$ python3.4 prime.py
bash-3.2$