简体   繁体   中英

Please help me modify my code for max min value

    def Goods(t):
        max_n = t.index(max(t))
        min_n = t.index(min(t))
        return max_n,min_n
    
    t = [-125,-164,1237,809,5634,1278,8431]
    Goods(t, len(t))

Hello. I'm trying to find two values ​​in a list and get an index tuple as the result. The two values ​​are the maximum and the minimum.

(6, 1)
>>> print(Goods([-125,-164,1237,809,5634,1278,8431]))
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    print(Goods([-125,-164,1237,809,5634,1278,8431]))
TypeError: Goods() missing 1 required positional argument: 'n'

I found the value. But what I want is a way to get results when I type "print(Goods([-125,-164,1237,809,5634,1278,8431]))" like this.

I would really appreciate it if you could teach me how to modify the code for the answer I want.

You didn't provide all of the arguments to your function. Should be:

def Goods(t, n):
    max_n = t.index(max(t))
    min_n = t.index(min(t))
    print((max_n,min_n))

buff_list = [-125,-164,1237,809,5634,1278,8431];
Goods(buff_list, len(buff_list))

If you would like to print the values, you have to ask the function to return something. Ie, modify your function to:

def Goods(t, n):
    max_n = t.index(max(t))
    min_n = t.index(min(t))
    return (min_n, max_n)

print(Goods(buff_list, len(buff_list)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM