Practice makes perfect median


#1



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

Oops, try again. median([1]) resulted in an error: object of type 'NoneType' has no len()


i expected it to return the median


def median(lists):
    lists = lists.sort()
    if lists == 1:
        return 1
    elif len(lists) % 2 == 0:
        median1 = len(lists) // 2
        median2 = median1 + 1
        median3 = (lists[median1] + lists[median2]) / 2.0
        return median3
    elif len(lists) % 2 == 1:
        median = len(lists) / 2
        median = lists[median]
        return median


#2

lists.sort() will return a sorted list, no need to store it in a variable.


#3

thank you but that doesn't answer the question.


#4

it should solve the problem you had?

If you need more help, post an updated version of your code + error message + question


#5

def median(lists):
    if len(lists) == 1:
        return 1
    elif len(lists.sort()) % 2 == 0:
        median1 = len(lists) // 2
        median2 = median1 + 1
        median3 = (lists[median1] + lists[median2]) / 2.0
        return median3
    elif len(lists.sort()) % 2 == 1:
        median = len(lists) / 2
        median = lists[median]
        return median

error:Oops, try again. median([4, 5, 5, 4]) resulted in an error: object of type 'NoneType' has no len()
the question is what does this message mean and how do i fix it?


#6

i was trying to say that:

lists.sort()

will give you a sorted list, so that you can leave out: lists =, since .sort() will update the list for you


#7

but what about the error message?


#8

What is the error message you receive in the exercise?


#9

this is the error message + and question.


#10

the first priority is to sort your list, using the .sort() method:

lists.sort()

or the built in sorted function:

lists = sorted(lists)

the problem was, that you combined this two approached by trying to save in a new variable after calling the .sorted() method, which won't work.


#11

in my new piece of code i didn't use sorted(), i only used .sort().


#12

yes, i know. But you use sorted wrong:

lists = lists.sort() # wrong use of sort method

As you can read in my answer


#13

that is not the point. the point is that in my new piece of code still has a few problems.


#14

but your new code is worse then your old code


#15

so how do i improve it?


#16

In your recent code you shared:

def median(lists):
    #where's sort?
    if len(lists) == 1: #No need for this. what if list is [4]? you will still return 1
        return 1
    elif len(lists.sort()) % 2 == 0: #sort shouldn't be here
        median1 = len(lists) // 2 #check indexing for these. indexes start at 0.
        median2 = median1 + 1 #same as comment above
        median3 = (lists[median1] + lists[median2]) / 2.0
        return median3
    elif len(lists.sort()) % 2 == 1: #sort shouldn't be here
        median = len(lists) / 2
        median = lists[median]
        return median

I hope you understand how to find the median manually. Just repeat those steps. The first step to finding median manually is to SORT the numbers correct? That should be the first thing you do in this code. And then you find the middle number.
As @stetim94 has mentioned, you can just simply put lists.sort() . Otherwise, you're pointing to the wrong items in the list (since it's unsorted).


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.