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))你好。我試圖在列表中查找兩個值并獲取索引元組作為結果。這兩個值是最大值和最小值。(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'我找到了價值。但我想要的是當我像這樣輸入“print(Goods([-125,-164,1237,809,5634,1278,8431]))”時獲得結果的方法。如果您能教我如何修改代碼以獲得我想要的答案,我將非常感激。
1 回答

呼如林
TA貢獻1798條經驗 獲得超3個贊
您沒有向函數提供所有參數。應該:
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))
如果你想打印這些值,你必須要求函數返回一些東西。即,將您的函數修改為:
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)))
添加回答
舉報
0/150
提交
取消