-1
def median(x):

    if range(1, len(x))%2!=0:
        sorted(x)
        listlength = range(1, len(x)+1)
        num = listlength / 2
        middlenum = x[num]
    else:
        sorted(x)
        listlength = range(1, len(x)) 
        num = listlength / 2
        num2 = num + 1
        middlenum = x[num2 + num / 2]
    return middlenum

This is my code, I need to find a median value of a list (x) but I'm getting this error :

"Oops, try again. median([1]) resulted in an error: unsupported operand type(s) for %: 'list' and 'int' "

I don't really know what to do, please help.

Uriel
  • 13,905
  • 4
  • 21
  • 43
Marcel Csharp
  • 19
  • 1
  • 5

3 Answers3

2

range(1, len(x)) is an iterable. It can not be divided by a number, 2 for that case.

I believe you meant to use simply if len(x) % 2:.

In adddition, sorted(x) will return x sorted, not sort it. For that task you have x.sort().


If you just want to find a median, use the built-in statistics.median:

>>> import statistics
>>> statistics.median([1, 2, 3])
2

Otherwise, see that answer.

Community
  • 1
  • 1
Uriel
  • 13,905
  • 4
  • 21
  • 43
  • def median(x): if len(x)%2!=0: x.sort() listlength = len(x)+1 num = listlength / 2 middlenum = x[num] else: x.sort() listlength = len(x) num = listlength / 2 num2 = num + 1 middlenum = x[num2 + num / 2] return middlenum Error : "Oops, try again. median([1]) resulted in an error: list index out of range "...... what am I doing wrong, DAMNIT, shet I just realised this code is super hard to read, like this – Marcel Csharp Jan 07 '17 at 17:22
  • 1
    Note also that in both branches of the `if` `listlength` will be another iterable (or a list in python 2.x), and again can't be divided by 2. Finally, the calculation in the even case should be `middlenum = (x[num] + x[num+1])/2` - the current calculation is going to end up calculating roughly the upper quartile! – Martin Bonner supports Monica Jan 07 '17 at 17:42
1

You could also use NumPy's built-in functions, which could potentially be faster.

import numpy as np
def median(x):
    return np.median(np.array(x))

NumPy has a whole suite of array-based data analysis functions, such as Mean, Mode, Range, Standard Deviation and more: https://docs.scipy.org/doc/numpy/reference/routines.statistics.html


Hope this helps!

Isky Mathews
  • 240
  • 1
  • 9
-1

In This program at first user will take five values.Then the values will be sorted and give the median value.It will also print the larger values and smaller values compared to the median.So, This is the code where you will get the median values.

a = [int(x) for x in input().split()]
p,q,r,s,t=a
w=a.sort()
median=a[2]
l=a[3],a[4]
m=a[0],a[1]
print("Median is : %i" %median)
print("The larger values are: %i %i" %l)
print("The smaller values are: %i %i" %m)

if user want to take many values then the code is:

import statistics
a = [int(x) for x in input().split()]
w=a.sort()
median=statistics.median(a)
print("Median is : %.2lf" %median)
Revel Yusuf
  • 25
  • 1
  • 5