我试图在函数中输入错误类型的数据时显示错误消息。在这种情况下,我只是在调用函数时尝试接受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)
相关分类