Discuss / Python / 作业

作业

Topic source
def findMinAndMax(L):
    if not L :
        return (None, None)
    min = max = L[0]
    for n in L:
        if not isinstance(n, (int, float)):
            raise TypeError('不支持除了数字外的列表')
        else:
            if n > max: max = n
            if n < min: min = n
    return (min, max)

>>>print(findMinAndMax([7, 1, 'a', 9, 0.05])) TypeError: 不支持除了数字外的列表 >>>print(findMinAndMax([])) (None, None) >>>print(findMinAndMax([55, 1, 155, 10, 0.05])) (0.05, 155)


None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False


  • 1

Reply