1 print('练习:请使用迭代查找一个list中最小和最大值,并返回一个tuple:') 2 def findMinAndMax(L): 3 if L !=[]: 4 (min,max)=(L[0],L[0]) 5 for x in L: 6 if max<x: 7 max=x 8 if min>x: 9 min=x 10 return(min,max) 11 else: 12 return(None,None) 13 14 15 16 17 if findMinAndMax([]) != (None, None): 18 print('测试失败!') 19 elif findMinAndMax([7]) != (7, 7): 20 print('测试失败!') 21 elif findMinAndMax([7, 1]) != (1, 7): 22 print('测试失败!') 23 elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9): 24 print('测试失败!') 25 else: 26 print('测试成功!')