仅返回元素列表中的整数似乎很容易,这是我的代码:
def return_only_integer(lst):
int_lst = []
for i in lst:
if type(i) == int:
int_lst.append(i)
return int_lst
在所有测试运行中都可以正常工作。但是当我更改它时:
def return_only_integer(lst):
int_lst = []
for i in lst:
if isinstance(i, int):
int_lst.append(i)
return int_lst
此测试未通过:
return_only_integer(["String", True, 3.3, 1])
出现此错误消息:
FAILED: [True, 1] should equal [1]
ERROR: Traceback:
in <module>
File "./frameworks/python/cw-2.py", line 28, in assert_equals
expect(actual == expected, message, allow_raise)
File "./frameworks/python/cw-2.py", line 18, in expect
raise AssertException(message)
cw-2.AssertException: [True, 1] should equal [1]
isinstance()
将布尔值True
提取为整数的两个代码段有什么区别? (假设我正确解释了此错误消息)