15. Median please Help!


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#

"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
        numlst.append(item)
    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
    else:
        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


#2

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


#3

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


#4

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


#5

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