猿问

调用具有错误输入类型的函数时打印“错误类型” - Python

我试图在函数中输入错误类型的数据时显示错误消息。在这种情况下,我只是在调用函数时尝试接受int或float。str在函数中输入 a应返回错误消息


def isPrime(i):


    if not (type(i)==float or type(i)==int):

        print("Input wrong type")

        return None


    i = abs(int(i))


    if i == 2 or i == 1: 

        return True    


    if not i & 1: 

        return False


    for x in range(3, int(i**0.5) + 1, 2):

        if i % x == 0:

            return False


    return True


# Wanting the code to return an error

isPrime(bob) 


弑天下
浏览 131回答 2
2回答
随时随地看视频慕课网APP

相关分类

Python
我要回答