def median(med_value):
med = []
value = str(med_value)
total = 0
for i in value:
med.append(i)
x= sum(med)
y = str(len(value))
total = total + y / x
return total

it returns error: median([1]) resulted in an error: unsupported operand type(s) for +: 'int' and 'str' What am I doing wrong? Thank you.

A string cannot be divided by an integer, only sliced.

I would like to speak about the variable names. Statistics has some fairly well defined concepts, and using variable names that reflect that is far more useful than just inventing them.

A sample space is a collection of data surveyed from a sample population. The median is the middle value of a sorted sample space. When two values fall in the middle, they are averaged. So I wouldn't begin by suggesting that the median is a list. It's a value.

Given that we are handed a list, we should not have value in the variable name. It is not semantic. When we describe the inputs we self-document the code.

Took me a while to edit the above, but the answer may be in there.

Let's examine the code in the OP...

def median(med_value):
med = []
value = str(med_value)
total = 0
for i in value:
med.append(i)
x= sum(med)
y = str(len(value))
total = total + y / x
return total

We know that the first line is removed.

Given that the parameter is a list, the second line will raise an exception. We need to use string formatting to represent a list as a string. The loop that follows assumes an iterable. A list is one already.

for i in med_value:

Here we stall. Problems solved means long explanations. Sometimes just looking at it solves it. Let's give that a try, first, eh?

I am so happy that I managed to do the median code without help and I would like to share my happiness with everyone here, so here is my very own code:

def median(median_value): #my very own code
median_value = sorted(median_value)
print median_value # I removed a for loop, which turned out, it was not necessary for this case.!
if len(median_value)% 2 == 0:
middle_2 = len(median_value)/2
middle_index_1 = median_value[middle_2]
middle_index_2 = median_value[middle_2-1]
middle_odd = (middle_index_1 + middle_index_2)/2.0
return middle_odd
elif len(median_value)% 2 != 0:
middle_1 = len(median_value)/2
middle_index = median_value[middle_1 ]
return middle_index
print median([5,68,12,13,2,9,1,8, 4, 50])
print median([1, 2, 5, 8, 9 ,7 ,5])

As I'm reading these forums, I'm noticing a lot of people are using a for loop to iterate over the items in the list and I'm not exactly sure why. Is there a reason people are using for loops?

@bakikovac your function seems to be finding the average and not the median.

Example: lst = [1,2,3,4,100] The median of this list will be 3, since it is the middle item of the list. The average would be 22.

As I am new, my answer might not be very satisfying but I will try my best to explain how I understand the for loop. - I only used codecademy to practically learn Python, so starting from early lesson you will build confidence about loops and how and when to use each. But I think so far for loop is the most used because it is easier to understand and it gets your code work. - for loop is very suitable if you are working with lists, dictionaries, strings or any type of series data as it allows you to iterate through the length of that data, through each item and also allows you to run the code contentiously until it reach the last item in "say" a list. for example you can make it sounds like this: for each item in my_list please divide each item by 2 and print the results or return the result, for loop will carry this mission until it reaches that last item in you list . with a lot of practice, you will have the feeling that for loop is the most understandable loop in python programming. I hope this was helpful.

Wow.!! thank you for pointing that out for me, as I am new to coding, I don't really see the shortest and efficient way for coding. Your code is super simple and easy. Thank you for sharing that. Best of luck with coding.