#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Python Error While Defining Function


    I am writing a code which takes a list and determines the median value. Here is what I've written. When I run it I get the following error:
    "TypeError: list indices must be integers, not float"

    Any ideas what is going wrong?

    (By the way the error refers to this line of code):
    c = ((myList[(y/2)] + myList[(y/2)-1]) / 2 )



    def median(myList):
    y = len(myList)
    if (y % 2 == 1):
    myList.sort()
    c = myList[int(y/2)]

    elif (y % 2 == 0):
    myList.sort()
    c = ((myList[(y/2)] + myList[(y/2)-1]) / 2 )

    else:
    c = "none"

    return c
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Try // instead of /
    With Python3 / will give floats.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by Dietrich
    Try // instead of /
    With Python3 / will give floats.
    This worked. Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo