Since your variable has the same misspelling, it isn't raising any exceptions, but I believe you meant to write:
length = ...
Consider a sample space with an odd number of items, as in odd length. The single item list would fit this category and does not need its own special case, so this is not needed:
We have one of two return possibilities. Either the middle term of an odd length sorted sample, or the average of the middle two terms if the length is even. This is a binary problem when we boil it down. One or the other.
This suggests that the simplest conditional won't even need an
Now just narrow down the return statements and you're done.
It's nice to see things broken down with the use of variables, but it becomes moot very quickly. There are no badges for verbose code, but if it helps you understand the components of the problem, then go ahead and use them. But expect yourself to go back and simplify your work. Don't just settle for the first thing that works. You'll learn more by revising and refining your code than you will ever learn if you stop there.
For this problem we only need three working variables.
- The sample space given in the parameter, sorted.
- The size or length of the sample space.
- The midpoint(s) of the sorted sample.
Adding more variables just complicates things, in my view, but as I said, if it helps in setting up the solution that's okay. Just go back and simplify afterward.
The sample space we already have, and if we do not have a need to preserve its original order, we can sort it in place without creating another list.
n = len(sample)
m = int(n / 2)
That's all we need to complete the solution. Now just write the returns and you're done.