15. median


#1



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


Oops, try again. median([1]) returned [1] instead of 1


Not entirely understanding the error message. Is my value returning as a string instead of an integer?


def median(numbers):
    total = 0
    sorted(numbers)
    if len(numbers) % 2 == 0:
        total = len(numbers) / 2
        numbers.remove(total)
        total = total + len(numbers) / 2
        total = total / 2.0
        print total
    elif len(numbers) == 1:
        total = numbers
        print total
    else:
        total = len(numbers) / 2.0
        print total
    return total


#2

you return a list, you should return an integer


#3

I have tried utilizing:

total = str(total)
total = int(total)

and the error i got was:
Oops, try again. median([1]) resulted in an error: invalid literal for int() with base 10: '[1]'

I tried this because I thought that str() would convert total to a string and from string I could use int() to convert the string into an integer.


Also tried:
total = int("".join(str(e) for e in total))

and the error was:
Oops, try again. median([4, 5, 5, 4]) resulted in an error: list.remove(x): x not in list

However, if I remove the int() from above attempt, I get this message:
Oops, try again. median([1]) returned '1' instead of 1


#4

to retrieve values from list, use square brackets:

aList[index]

#5

so I tried placing total = total[0] before print and got:
Oops, try again. median([1]) resulted in an error: 'int' object has no attribute 'getitem'

I'm sure I am missing something simple, but just not seeing it.


#6

why use fixed values? By using the list length divided by 2 you can get the middle index, which you can then use for getting the value.

If you need more help, post an updated version of your full code


#7

def median(numbers):
    total = 0
    sort = sorted(numbers)
    
    if len(numbers) % 2 == 0:
        mid = len(sort) / 2
        total = mid + (mid - 1)
        total = total / 2.0
        print total
        
    elif len(numbers) == 1:
        total = total[sort]
        print total
        
    else:
        center = len(sort) / 2.0
        total = total[center]
        print total
        
    return total

#8

why do you make a special case for when the list length is one? length (1) divided by 2 gives zero, which is the index you need, so i don't see why you make a case for list length 1

What help do you expect of me?


#9

okay i deleted the instance for length (1)

My code still throws errors and I am confused as to how to fix it, I have been reviewing other posts on the same problem and attempted several changes with no avail.


#10

here:

center = len(sort) / 2.0

you are going to use center as index, why divide by 2.0? index values are always integers, never floats.

piece of advise, add a function call:

def median(numbers):
    total = 0
    sort = sorted(numbers)
    
    if len(numbers) % 2 == 0:
        mid = len(sort) / 2
        total = mid + (mid - 1)
        total = total / 2.0
        print total
        
    elif len(numbers) == 1:
        total = total[sort]
        print total
        
    else:
        center = len(sort) / 2.0
        total = total[center]
        print total
        
    return total

print median([1])

so you can see what is happening, it will now also show intepeter errors which might help you find the problem


#11

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