请使用迭代查找一个list中最小和最大值,并返回一个tuple
代码:
def findMinAndMax(L): if L == []: return (None, None) else: max = L[0] min = L[0] for i in L: if i > max: max = i if i < min: min = i return (min, max)
测试代码:
if findMinAndMax([]) != (None, None): print('测试失败!') elif findMinAndMax([7]) != (7, 7): print('测试失败!') elif findMinAndMax([7, 1]) != (1, 7): print('测试失败!') elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9): print('测试失败!') else: print('测试成功!')
本文来自博客园,作者:anthinia,转载请注明原文链接:https://www.cnblogs.com/anthinia/p/10930370.html