我正在研究一个python程序,找到一个范围内的完美正方形的所有数字,并且数字中所有数字的总和小于10
我的测试用例显示断言错误
AssertionError: None != [1, 4, 9, 16]
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
为什么会发生这种情况
def perfSq(l,u):
a=[]
for x in range(l,u+1):
if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10:
a.append(x)
print a
import unittest
class Test(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testPerfSq(self):
self.assertEqual(perfSq(1,20) , [1,4,9,16] )
if __name__ == '__main__':
unittest.main()
它有什么不对吗?
答案 0 :(得分:0)
perfSq
返回None
。您正在将None
与[1, 4, 9, 16]
进行比较
更改
def perfSq(l,u):
a=[]
for x in range(l,u+1):
if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10:
a.append(x)
print a
到
def perfSq(l,u):
a=[]
for x in range(l,u+1):
if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10:
a.append(x)
print a
return a