创建一个查看三个参数的函数

2024-10-03 09:20:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个函数来查看三个参数,如果它们都是同一类型,则返回布尔真值,否则返回假值:

到目前为止,我的情况是:

def whattype(n):
    if type(n) is int:
        print "True"
    elif type(n) != int:
        print "False"
whattype("car")
whattype(1)
whattype(2)

Tags: 函数true类型参数ifisdeftype
1条回答
网友
1楼 · 发布于 2024-10-03 09:20:54
def whattype(a, b, c):
    return type(a) == type(b) == type(c)

解释首先,函数必须有三个参数(这里是abc)。如果要返回值,必须使用return语句。此外,可以在一行中进行多次比较,因此可以检查一行中类型的相等性并立即返回结果

您可以使用如下函数:

>>> whattype(1, 2, "car")
False
>>> whattype(1, 2, 3)
True

相关问题 更多 >