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?action=resume

<In what way does your code behave incorrectly? Include ALL error messages.>

On line 7 my code is returning an invalid syntax error when it was working before. Before however, it was returning the wrong number when it functioned.

<What do you expect to happen instead?>
I expected it to return the correct number.

```python

def median(values):
sorted(values)
if len(values) <= 1:
return values[0]
elif len(values) % 2 == 0:
return float((values[len(values)/2] + values[(len(values)/2)- 1]) / 2.0
elif len(values) % 2 != 0:
return values[(len(values)/2)]

<do not remove the three backticks above>

#2

check your open and closing parentheses in the return float line


#3

They balance out so that shouldn’t be a problem.


#4

The above line is not doing anything. It should be assigned.

s = sorted(values)

Not necessary. The odd case will include a length of 1.

No conditional required. Can be else:

Since the float is explicitly declared, 2.0 should just be 2 (a counting number).

Repeated expressions may be cached to aid in readability.

n = len(s)
m = int(n / 2)

#5

I see! The “else” component is what was throwing it off. Also, thank you for the readability tip; made the coding much easier :+1:


#6

5 open parentheses in that line, 4 closing.


#7

Right you are. I missed one. I think once the OP simplified the code that problem would have surfaced.


#8

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