Median


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#

<In what way does your code behave incorrectly? Include ALL error messages.>
Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5

<What do you expect to happen instead?>
it should return 4.5

```python

def median(num):
total = len(num)
sort= sorted(num)

if total%2 ==0:
    med1= sort[int(total/2)] 
    med2 = sort[int((total/2) - 1)]
    average = (med1+med2)/2
    return average
    
elif total==1:
    return sort[0]
else:
    k = int(c-0.5)
    return sort[k]
<do not remove the three backticks above>

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

#2

when the division involves only integer (9 divided by 2) python will floor (round down) the result to the nearest integer

to overcome this problem, the division should involve a float


#3

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