```
def median(x):
if not isinstance(x, list): return False
k = len(x)
if k == 0: return False
elif k == 1: return x[0]
x.sort()
n = k / 2 # n is an integer
if k % 2 > 0: return x[n]
return float(x[n-1] + x[n]) / 2 # return is a float
```

This is not meant as lesson code, just a distillation of yours with a couple of validation checks. The one check that is missing is the check for all numbers in the list.

We can do the check any number of ways. This is one way I came up with just now...

```
import re
def median(x):
if not is_nums(x): return False
k = len(x)
if k == 0: return False
elif k == 1: return x[0]
x.sort()
n = k / 2
if k % 2 > 0: return x[n]
return float(x[n-1] + x[n]) / 2
def is_nums(x):
if not isinstance(x, list): return False
h = [ n for n in x if re.match(r'\d',str(n))]
return len(h) == len(x)
```

Since sorting the list will sort the global object, this is not a pure function. Things get mutated that are not local to the function. We can make it a little more pure by taking a shallow copy of the input list, and work on it, thereby leaving the global object unaffected.

This line,

`x.sort()`

gets replaced by,

`y = sorted(x)`

or

```
y = x[:]
y.sort()
```

then these two lines,

```
if k % 2 > 0: return x[n]
return float(x[n-1] + x[n]) / 2
```

become,

```
if k % 2 > 0: return y[n]
return float(y[n-1] + y[n]) / 2
```

The hand-off comes with a slight semantics revision to stave off any questions...

```
def median(x):
if not is_nums(x): return False
k = len(x)
if k == 0: return False
elif k == 1: return x[0]
else: pass
y = sorted(x)
n = k / 2
if k % 2 > 0: return y[n]
return float(y[n-1] + y[n]) / 2
```