好的,这是我的代码:
def is_prime(n):
n = abs(int(n))
if n < 2:
return False
elif n == 2:
return True
elif n%2 == 0:
return False
else:
prime(n)
def prime(n):
for x in range(3, int(n**0.5)+1,2):
if n%x == 0:
return False
else:
return True
print is_prime(6577)
但每当我在我的shell中运行它时它返回'None',我不明白为什么。任何帮助将不胜感激。
答案 0 :(得分:1)
else
中的最终is_prime
不返回任何内容。您甚至可以完全删除else
,但这只是个人偏好
def is_prime(n):
# You don't really need to take abs value cause you already do a check for < 2 which includes negative numbers
n = abs(int(n))
if n < 2:
return False
elif n==2:
return True
elif n%2 == 0:
return False
return prime(n)