15/15 "else:" returning syntax error


#1

def median(numlist):
    length = len(numlist)
    location = length/2
    if length % 2 == 0:
        return sorted(numlist(location) + sorted(numlist, reverse=True)(location) / 2
    else:
        return numlist(location+5)

#2

try this here

def median(numlist):
    # i just sorted the list here
    #instead of doing at the time of
    #the median computuation
    numlist.sort()
    length = len(numlist)

    # this division will truncate all
    #decimal ponts to 0 but will still
    #give you a float
    #make it int
    location = int(length/2)
    print(location)
    if length % 2 == 0:
        #accessing a list by this notation numlist(index) is
        #wrong
        #rather it like this numlist[index]
        return (numlist[location] + numlist[location-1]) / 2.0
    else:
        return numlist[location]


print (median([4,5,5,4,5]))

#3

thankyou, i had considered simplifying the if section.
completely forgot about the int operator though. :wink:


#4

@rydan

i've been using Q&A so far, and I can't thank you enough for your contribution. Your answers are neat and easy. Can I ask how you could be that good?
How did you study? Let us know.


#5

@medouxa I am still learning to be an even better advanced programmer. Generally I try to solve a lot of problems in the various languages I know.I Read a lot of books on those languages an write projects in those languages too. I have had professional experience in Java, PHP and python and I also learnt a lot from them