15. Median please Help!



"Oops, try again.
median([1]) resulted in an error: global name 'numlist' is not defined".

This is the error i keep on getting. I have checked over the code a few times and havent figued it out yet.

def median(numbers):
    numlst = []            # new list to hold input numbers
    for item in numbers:   #Adding numbers to the new list
    median = 0        #empty variable for holding the median
    numlst.sort()          #Sorting the list
    numlen = len(numlist) #getting the length of the list
    if numlen % 2 == 0:   #Check if length is an even number
        i = numlen / 2.0  #get position of first element. 
        round(i, 1)       # round off to get whole number
        median = (numlist[i] + numlist[i - 1]) /2.0
        if numlen % 2 != 0: #check if its an odd number
            i = round((numlen / 2.0), 1) 
            median = numlst[i - 1] #get middle element
    return median


you seem to have two variable:
numlst which you create here:

numlst = []

but then here:

numlen = len(numlist)

you start using numlist (with i), which hasn't exist before


snap! I hadnt noticed that. Thank you!! let me correct that.


first try to rule out further problems yourself (given you are now a step further), obviously if more help is required you can post a reply with an updated version of your code


The code worked after formatting the list indices to Int and removing the float formatting.
Thanks for the help @stetim94