I recently wanted to test out my fresh Python skills by doing a Codecademy Python course. I really like the way they explain things and even a complete beginner can start comfortably not just by reading trough pages of documentation, but by seeing examples and putting them to use on their online interpreter. I’ll be doing other courses they have over there for sure, and I highly recommend it, if you’re exploring a new language or are completely new to programming.
During a final exercise called median (under Loops / Practice Makes Perfect) I stumbled upon a first anomaly that I met up until now. The instructions are as follows:
Write a function called median that takes a list as an input and returns the median value of the list.
For example: median([1,1,2]) should return 1.
- The list can be of any size and the numbers are not guaranteed to be in any particular order.
- If the list contains an even number of elements, your function should return the average of the middle two.
I wrote the function, but line of code that calculated the average of two numbers didn’t quite work:
return (int(sorted_lst) + int(sorted_lst)) / 2