Please check for errors in the code


Oops, try again. median([4, 5, 5, 4]) resulted in an error: 'int' object is not iterable

def median(nos):
    for i in sort:
        if i%2==0:
            for k in i:
                return int((k+(j+1))/2)
        elif i==1:
            return 1
            for k in i:
                return int(k/2)


for k in i:

What is i?


The list items are integers and integers are not iterable data type, so we have this error.

A simple logic to solve this problem could be this.

1.Sort the list
2.Check if list have even or odd numbers of items in it.
3.If It has even numbers of items, we will take average of two mid items.
4.If it has odd number of items, then you have to just return the middle item.

Also can you define your logic to solve this problem in simple words?, that'd help us debug the code.


Thanks for your help. I figured out the problem.


I hope it's clear why last code did not worked out,also we did not needed loops at all.
If You feel doubt over any of this, You can point them out here. :thumbsup:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.