1
重要提示:我不能在我的minmax
功能使用min()
和max()
內置功能。創建一個極大極小函數接收的參數列表,並返回一個元組(最小值,最大值)
這裏是我的代碼中發現的錯誤:
expression « minmax([-1000, -999]) » doesn't have expected value
expected value : (-1000, -999)
actual value : (-1000, -1000)
這裏是我的代碼:
def minmax(liste):
if len(liste)==1:
return (liste[0],liste[0])
elif len(liste)==0:
return(None,None)
else:
min=liste[0]
for i in liste:
if i<min:
min=i
max=liste[0]
for k in liste:
if k>=max:
max=k
return(min,max)
會照顧_why_? – Lafexlos
因爲return語句在for循環中,所以總是返回第一個元素。需要將return語句放在外面。 允許循環完成 –
然後,您應該添加到您的答案,使其更好。只寫「做這個」不是回答問題的好方法。 – Lafexlos