Discuss / Python / 迭代练习

迭代练习

Topic source
# -*- coding: utf-8 -*-def findMinAndMax(L):    if len(L) == 1:        return (L[0], L[0])    elif len(L) == 0:        return (None,None)    else:        a = L[0]        for i in L:            if i < a:                a = i        b = L[0]        for i in L:            if i > b:                b = i        return (a, b)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('测试成功!')

  • 1

Reply