Median exercise (practice makes perfect): https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096
In my code for the exercise I keep getting 'invalid syntax' errors in different places. I do get the error if I want to use 'else'/'elif' for the second condition in the loop (count%2), but not if I use another 'if' (as in the code below).
Also, I do get the invalid syntax error if I want to return median2, as it is in the code below now, but not if I don't define median2 but instead return its content directly (as in return (sorted_list[len(sorted_list)/2.0] + sorted_list[((len(sorted_list)/2.0)+1)*0.5]).
Then, however I receive the invalid syntax error for my lisst definition (lisst=[4,4,5,5]).
What am I doing wrong here? What is the reason for these errors and why do they sometimes disappear (as I described above)?
Many thanks for help.
def median(lisst): sorted_list = sorted(lisst) count=0 for i in range(len(sorted_list)): count+= 1 return count if count%2 != 0: median1 = sorted_list[(len(sorted_list)+1)/2.0] return median1 if count%2 == 0: median2 = (sorted_list[len(sorted_list)/2.0] + sorted_list[((len(sorted_list)/2.0)+1)*0.5] return median2 lisst = [4,4,5,5] print median(lisst)