试图让一大块代码工作,而我的一个测试用例失败了。它应该返回:
Invalid Point! (7, 8, 9, 10)
Invalid Point! ('hi', 2, 3)
((1, 2, 3), (4, 5, 6))
相反,我得到了标题中提到的TypeError和Invalid Point! (1, 2, 3)
以下代码供参考。
def collection_check(a_coll, coll_template):
#Receives a collection to check and a template collection to check against. The procedure
#raises errors if the collection to check is the incorrect type, is the wrong length, or
#contains data types that do not match the data types of the provided template.
if not type(a_coll)==type(coll_template):
out_str = "TypeError:{} and template are different classes."
raise TypeError(out_str.format(a_coll))
if not(len(a_coll)==len(coll_template)):
out_str = "ValueError:{} and template are different lengths."
raise ValueError(out_str.format(a_coll))
for i in range(len(a_coll)):
if not(type(a_coll[i])==type(coll_template[i])):
out_str = "TypeError:{} is not type {}"
raise TypeError(out_str.format(a_coll[i],type(coll_template[i])))
def clean_points(point_tup):
#Receives a tuple of tuples containing values and returns a new nested tuple with all
#of the original tuples that contain exactly three numbers.
new_points = []
for point in point_tup:
try:
collection_check(point_tup[point],(0,0,0))
new_points.append(point[:])
except (TypeError,ValueError) as e:
print("Invalid Point! {}".format(point))
break
return list(new_points)
###
#Test cases:
# >>> clean_points(((1,2,3),(4,5,6),(7,8,9,10),('hi',2,3)))
# Invalid Point! (7, 8, 9, 10)
# Invalid Point! ('hi', 2, 3)
# ((1, 2, 3), (4, 5, 6))
# >>> clean_points((('h',0),(1,1,1),(2,2,2)))
# Invalid Point! ('h', 0)
# ((1, 1, 1), (2, 2, 2))
###
怎么回事?代码中还有其他问题需要解决,但这是我发现的第一个障碍。
修改代码以打印错误消息最终显示了您在标题中提到的错误消息:
def collection_check(a_coll, coll_template):
#Receives a collection to check and a template collection to check against. The procedure
#raises errors if the collection to check is the incorrect type, is the wrong length, or
#contains data types that do not match the data types of the provided template.
if not type(a_coll)==type(coll_template):
out_str = "TypeError:{} and template are different classes."
raise TypeError(out_str.format(a_coll))
if not(len(a_coll)==len(coll_template)):
out_str = "ValueError:{} and template are different lengths."
raise ValueError(out_str.format(a_coll))
for i in range(len(a_coll)):
if not(type(a_coll[i])==type(coll_template[i])):
out_str = "TypeError:{} is not type {}"
raise TypeError(out_str.format(a_coll[i],type(coll_template[i])))
def clean_points(point_tup):
#Receives a tuple of tuples containing values and returns a new nested tuple with all
#of the original tuples that contain exactly three numbers.
new_points = []
for point in point_tup:
try:
collection_check(point_tup[point],(0,0,0))
new_points.append(point[:])
except (TypeError,ValueError) as e:
print(e) # this will show the true error ========== right here
print("Invalid Point! {}".format(point))
break
return list(new_points)
您正试图用tuple访问元组,这对python来说没有意义,因为您需要用整数索引访问元组。当像这样循环时
for point in point_tup:
点变量包含point_tup
变量中的第i个元组,在测试用例之后,点在每次迭代中都将等于以下
(1, 2, 3)
(4, 5, 6)
(7, 8, 9, 10)
('hi', 2, 3)
所以你不需要像这个那样得到点元组
point_tup[point]
你已经在point
变量中有了它
您的问题在于这些行:
for point in point_tup:
try:
collection_check(point_tup[point],(0,0,0))
point
是一个元组(通过point_tup
迭代而分配,而不是整数,因此您不能使用它来索引到point_tup
。很可能您只想要collection_check(point, (0, 0, 0))
。