2
  1. The error message is:

    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

  2. My code

    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!!')
    

2 Answers 2

2

Simply remove the comma in the for loop:

...
for i in L:
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

I feel so embarrassed that I made such a stupid mistake.Anyway,thanks a lot.^_^
1

I'd rather use a different code:

def findMinAndMax(L):
    if L == []:
        return (None, None)
    return (min(L), max(L))

Simple, isn't it?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.