请使用迭代查找一个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('测试成功!')

 

posted @ 2019-05-27 13:57  anthinia  阅读(174)  评论(0编辑  收藏  举报