2024-10-03 09:20:54 发布
网友
我想创建一个函数来查看三个参数,如果它们都是同一类型,则返回布尔真值,否则返回假值:
到目前为止,我的情况是:
def whattype(n): if type(n) is int: print "True" elif type(n) != int: print "False" whattype("car") whattype(1) whattype(2)
def whattype(a, b, c): return type(a) == type(b) == type(c)
解释首先,函数必须有三个参数(这里是a、b和c)。如果要返回值,必须使用return语句。此外,可以在一行中进行多次比较,因此可以检查一行中类型的相等性并立即返回结果
a
b
c
return
您可以使用如下函数:
>>> whattype(1, 2, "car") False >>> whattype(1, 2, 3) True
解释首先,函数必须有三个参数(这里是
a
、b
和c
)。如果要返回值,必须使用return
语句。此外,可以在一行中进行多次比较,因此可以检查一行中类型的相等性并立即返回结果您可以使用如下函数:
相关问题 更多 >
编程相关推荐