15/15 error


#1


#2

Post the code, please, so we can run it. Thank you.


#3

def median(lst):
    new_lst = []
    new_lst = sorted([new_lst])
    if len(new_lst) % 2 == 0 and len(new_lst) > 1:
        for i in new_lst:
            x = len(new_lst) / 2
        return float((i[x] + i[x+1]) / 2)
    else:
       for i in new_lst:
            x = (len(new_lst) / 2) - (len(new_lst) % 2)
        return i[x+1]
print median([5,5,3,2,6,8,4,2,8,6,])

Oops, try again. median([1]) resulted in an error: list index out of range


#5

sorry i just updated post 3 to my current formatted code... i changed that line.
still not working tho


#6

Formatting doesn't change the code, just the readability. Please dig around for a thread about formatting code samples and phrases; and, please post code. It's the cat's meow for getting quick help, as my experiments continue to prove.


#7

by format i meant I have been trying different things in the past 40 min. and simply changed post three to reflect that rather than making more posts... so since i posted my original post I have tried adding a "float" as well as changing "new_lst = sorted([lst])" to new_lst = sorted([new_lst)]
however neither of these changes have helped.


#8

We need to go back to the beginning. Please post what you have of code at present, in a new Reply, and we can go from there. Thank you.


#9
def median(lst):
new_lst = []
new_lst = sorted([new_lst])
if len(new_lst) % 2 == 0 and len(new_lst) > 1:
    for i in new_lst:
        x = len(new_lst) / 2
    return float((i[x] + i[x+1]) / 2)
else:
   for i in new_lst:
        x = (len(new_lst) / 2) - (len(new_lst) % 2)
    return i[x+1]

print median([5,5,3,2,6,8,4,2,8,6,])


#10

Everything below "def median(lst):" is indented, except print... not sure why it didn't show up that way


#11

Three back-ticks on a an own line before code block sample, and three after, also on an own line.


#12

@ardosso18 Here is you code with explanations on why it is not working and you are getting the errors

def median(lst):
    
    #Here you are creating a new_lst(list) an making it empty
    #this is the list you are making all your queries from
    #meaning no matter what the input list you pass to this method
    # it will always process an empty list
    #you will get an "IndexError: list index out of range" message
    #because there is nothin in this list
    
    new_lst = []
    
    # you are sorting an empty list
    new_lst = sorted([new_lst])
    
    #nice check but your code will never go into
    ##this block because there because the length of 
    #new_lst is 0 and will never be greater than 1
    
    if len(new_lst) % 2 == 0 and len(new_lst) > 1:
        
        #if your code were to execute this, here is the pitfall
        #Remember when we create a for loop over an iterable directly
        # an iterator which has the same type as the elements in the list is created
        #in this case the iterator will be an int
        
        for i in new_lst:
            
            #awesome
            x = len(new_lst) / 2
        
        # you will get this error here
        #"TypeError: 'int' object is not subscriptable"
        #you can only access iterables by indices and since
        #ints and floats are not iterables you get that error
        return float((i[x] + i[x+1]) / 2)
    else:
        #implications for this discusssed earlier
        for i in new_lst:
            x = (len(new_lst) / 2) - (len(new_lst) % 2)
    # same pitfall discussed earlier
    return i[x+1]
print median([5,5,3,2,6,8,4,2,8,6,])

Now to correct your code there are a few pointers here you do not need the for loop in side your code because list elements can be access by indices which is a constant time operation.

Here is your code corrected

def median(lst):
    # you have to sort the input list instead
    #not an empty one
    # you have to write it like this
    #new_lst = sorted(lst) not new_lst = sorted([lst])
    #now in all of your queries you will be using new_lst
    new_lst = sorted(lst)
    
    #Okay you choose to check it like this awesome
    if len(new_lst) % 2 == 0 and len(new_lst) > 1:
        # the midpoint of the list
        x = len(new_lst) / 2
        ##there was a change here take a look
        
        return (new_lst[x-1] + new_lst[x]) / 2.0
    else:
        x = (len(new_lst) / 2) - (len(new_lst) % 2)
    return new_lst[x+1]
print median([5,6,5,3,7,8,4,8,5,7])