gpt4 book ai didi

python - 使用迭代查找最大值和最小值时的Python TypeError

转载 作者:行者123 更新时间:2023-12-03 07:40:46 25 4
gpt4 key购买 nike

  • 错误消息是:

    Traceback (most recent call last):

    File "C:/1-Python/2-projects/最值.py", line 18, in elif findMinAndMax([7]) != (7, 7):

    File "C:/1-Python/2-projects/最值.py", line 7, in findMinAndMax for i, in L:

    TypeError: cannot unpack non-iterable int object

  • 我的代码
    def findMinAndMax(L):
    if L == []:
    return (None, None)
    else:
    max1 = L[0]
    min1 = L[0]
    for i, in L:
    if i >= max1:
    max1 = i
    elif i <= min1:
    min1 = i

    return (min1, max1)


    if findMinAndMax([]) != (None, None):
    print('test failed!')
    elif findMinAndMax([7]) != (7, 7):
    print('test failed!!')
    elif findMinAndMax([7, 1]) != (1, 7):
    print('test failed!!')
    elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
    print('test failed!!')
    else:
    print('test success!!')
  • 最佳答案

    只需在for循环中删除逗号:

    ...
    for i in L:
    ...

    关于python - 使用迭代查找最大值和最小值时的Python TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59477014/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com