python第五章程序练习题

5.2

1 def isOdd(a):
2     if a%2!=0:
3         return True
4     else:
5         return False7 a=eval(input())
8 print(isOdd(a))

5.3

1 def isNum(x):
2     try:
3         x=eval(x)
4         return True
5     except NameError:
6         return False8 x=input()
9 print(isNum(x))

5.4

1 def multi(*a):
2     m=1
3     for x in a:
4         m=m*x
5     return m7 x=input().split()
8 lst=[int(i) for i in x]
9 print(multi(*lst))

5.5

 1 from math import sqrt
 2 def isPrime(n):
 3     if n == 1:
 4         return False
 5     for i in range(2,int(sqrt(n))+1):
 6         if n % i == 0:
 7             return False
 8     return True
 9 try:
10     n = eval(input())
11     print(isPrime(n))
12 except:
13     print("输入错误,请输入一个数字")

 

你可能感兴趣的:(python第五章程序练习题)