Please check for errors in the code


#1



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


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


#2

for k in i:

What is i?


#3

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.


#4

Thanks for your help. I figured out the problem.


#5

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:


#6

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