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/courses/learn-python/lessons/practice-makes-perfect/exercises/median
<In what way does your code behave incorrectly? Include ALL error messages.>
Answer give 5.0 instead of 4.5

<What do you expect to happen instead?>

However solved when i use second code.



Replace this line with your code. 
code 1:
def median(a):
  sorted(a)
  if len(a) % 2 == 0:
    return (a[(len(a)/2)-1] + a[len(a)/2])/2.0
  else:
    return a[(len(a)/2)]

code 2:
def median(b):
  a = sorted(b)
  if len(a) % 2 == 0:
    return (a[(len(a)/2)-1] + a[len(a)/2])/2.0
  else:
    return a[(len(a)/2)]


#2

sorted() doesn’t directly modify a list, it returns a sorted copy of the list. you have to assign that to something, that’s why the 2nd code worked you assigned sorted(b) to a


#4

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