CodeWars 问题:
Create a function named divisors that takes an integer and returns an array with all of the integer's divisors(except for 1 and the number itself). If the number is prime return the string '(integer) is prime'
当我在 Spyder3 for Python 中测试我的下面代码时,它运行完美。
def divisors(integer):
arr = []
for x in range(2,integer - 1): #if integer is 12
if integer % x == 0:
arr.append(x)
if len(arr) == 0:
print(integer, 'is prime')
else:
print(arr)
但是,当我将它提交给 CodeWars 时,它返回以下错误:
divisors(15) 应该返回 [3, 5];我的功能就是这样做的。 CodeWars 窗口日志显示答案是正确的,但继续说 None should equal [3, 5]。
我检查它是否返回列表以外的其他内容,但检查出来了。有人能发现问题吗?
请您参考如下方法:
该站点希望您从函数返回一个值 - 而您只是打印它:
if len(arr) == 0:
return str(integer) + ' is prime'
else:
return arr
