timzorr

November 23rd, 2010, 01:31 AM

n = float(input("Enter the size of the list "))

i = 0

k = n - 1

numericValues = [0]

numericValues[0] = float(input("Enter list element "))

while (i < k):

i = i + 1

numericValues.append( float(input("Enter list element ")))

theValues = sorted(numericValues)

if len(theValues) % 2 == 1:

print(theValues[(len(theValues)+1)/2-1])

else:

lower = theValues[len(theValues)/2-1]

upper = theValues[len(theValues)/2]

print((lower + upper) / 2)

Im trying to find the median of a list of numbers but im getting the error code:

lower = theValues[len(theValues)/2-1]

TypeError: list indices must be integers, not float

This error only appears in python3 because i have tested it in python 2.6.6 and it worked without any errors. :confused:

i = 0

k = n - 1

numericValues = [0]

numericValues[0] = float(input("Enter list element "))

while (i < k):

i = i + 1

numericValues.append( float(input("Enter list element ")))

theValues = sorted(numericValues)

if len(theValues) % 2 == 1:

print(theValues[(len(theValues)+1)/2-1])

else:

lower = theValues[len(theValues)/2-1]

upper = theValues[len(theValues)/2]

print((lower + upper) / 2)

Im trying to find the median of a list of numbers but im getting the error code:

lower = theValues[len(theValues)/2-1]

TypeError: list indices must be integers, not float

This error only appears in python3 because i have tested it in python 2.6.6 and it worked without any errors. :confused: