我現在這裏有一個工作功能:在第二個函數中調用布爾函數? - Python的
def median(alist):
copylist = alist[:]
copylist.sort()
if len(copylist)%2 == 0:
rightmid = len(copylist)//2
leftmid = rightmid - 1
median = (copylist[leftmid] + copylist[rightmid])//2
else:
mid = len(copylist)//2
median = copylist[mid]
return median
,我想這個函數來調用第二功能ISEVEN第一功能。這是第二個功能:
def isEven(n):
if n % 2 == 0:
return True
else:
return False
這是正確的方法來做到這一點,或是有另一種縮短中位數函數中的代碼?
def median(alist):
copylist = alist[:]
copylist.sort()
if isEven(len(alist)) == True:
rightmid = len(copylist)//2
leftmid = rightmid - 1
median = (copylist[leftmid] + copylist[rightmid])//2
else:
mid = len(copylist)//2
median = copylist[mid]
return median