【发布时间】:2023-04-10 12:06:01
【问题描述】:
当我运行这段代码时;即使列表只包含数字,当它应该返回[1, 2, 3] 时,它仍然作为错误返回。我该如何解决这个问题?
def check_integer(a, b, c):
if type([a, b, c]) != int:
raise TypeError("Must be numbers.")
else:
return [a, b, c]
print (check_integer(1, 2, 3))
【问题讨论】:
-
您使用
is关键字而不是==检查类型 -
您正在传递一个列表并检查它是否为 int。你的逻辑不正确
-
type([1,2,3])是list