1、实现isOdd()函数,参数为整数,如果整数是奇数,返回True,否则返回False
def isOdd(num):
if num % 2 == 0:
return True
else:
return False
n = eval(input())
print(isOdd(n))
2.实现isNum()函数,参数为一个字符串,如果这个字符串输入整数、浮点数或复数的表示,则返回Ture,否则返回False
def isNum(word):
try:
words = type(eval(word))
if words == type(1):
return True
elif words == type(1.0):
return True
elif words == type(1+1j):
return True
except:
return False
n = input()
print(isNum(n))
3.实现multi()函数,参数个数不限,返回所有参数的乘积
def multi(*b):
a = 1
for i in b:
a *= i
return a
print(multi(1,2,3))
4.实现 isPrime()函数,参数为整数,要有异常处理。如果整数是质数,返回Ture,否则返回False
from math import *
def isPrime(num):
try:
num = eval(num)
if type(num) == type(1):
if num == 1:
return False
else:
for i in range(2,int(sqrt(num)+1)):
if num % i == 0:
return False
return True
except:
print("输入有误!请输入整数")
if __name__ == '__main__':
n = input("请输入整数:")
isPrime(n)