def median(list):b = len(list)if b % 2 != 0: a = list<(b / 2) - 0.5>else: a = ( list<(b / 2)> + list<(b / 2) - 1 >) / 2return aHello,I keep gaining ‘Your code threw a “list indices need to be integers, not float” error’ error. I confirm it numerous times and also can’t check out where that is not an integer..
You are watching: Typeerror: list indices must be integers or slices, not float
Double answer Explanation
def median(List): newList = sorted(List) count = len(newList) if count % 2 == 0: return (float(newList< (count/2) >) + float(newList< ( ( count/2 ) - 1 ) >)) / 2 else: return float(newList< (count/2) >)print median(oldList)Now, the above looks clean but it’s hard to review to number out those going on. So take a look in ~ this instead, together ALL I’ve done is do it more readable:
oldList = <3,2,1,4,5,6,42,51,23,52,3,6,1,2,4,6,412,54,22,125,33>def median(List): newList = sorted(List) count = len(newList) high = float(newList< (count/2) >) short = float(newList< ( ( count/2 ) - 1 ) >) print "Here is our organized list", newList publish "Here space how many items we have actually within the list", count print "Here we have our typical on odd lists and our upper number for even ones", high print "Here we have our mean number because that odd lists", low publish "Here we have actually a an unified average that high and low", (high + low) / 2 if counting % 2 == 0: return (high + low) / 2 else: return highprint median(oldList), "Final Answer"
Play through either here:Short: http://labs.incubadoradeartistas.com/B1Xd/3#:workspaceDemonstration: http://labs.incubadoradeartistas.com/B1Xd/2#:workspace
And ns shall describe what’s going onDefine a function called median and use the parameter ListCreate an organized list dubbed newList through sorting the values of List before using them. This means we keep our oldList accessible for various other functions.Define a variable dubbed count which find the len*gth that the *listIf count is an even number…Return… (here come the doozy explanation, for this reason I’ll an outbreak of this list style)
We require to discover the floated typical of 2 numbers, because there is no revolution calculated typical in an also amount that numbers.
Pretend there room 6 items in the list, together such: 1, 3, 7, 11, 13, 23
Because we room targeting an index (ie < >) the framework starts at 0 (ie <0,1,2…>)But as soon as we run count, the structure starts in ~ 1 (ie. 1,2,3…)
Keep in mind we’re tracking the index number we need, therefore this mathematics is acting as a way to uncover which ITEM we need, not what number.
( How plenty of items / 2 ) Which equals 3 in this case 6 / 2 = 3rd Index( How countless items / 2 - 1 ) Which amounts to 2 in this instance 6 / 2 - 1 = 2nd Index
index<3> = 4th item (value) +index<2> = third item (value) / 2
Which returns the middle two numbers averaged together.
See more: Do Traded Pokemon Level Up Faster Or Does That Only Work With Other Players?
Short: http://labs.incubadoradeartistas.com/B1Xd/3#:workspaceDemonstration: http://labs.incubadoradeartistas.com/B1Xd/2#:workspace
!<1>when you article code, to mark it all and click the -button in bespeak to keep formatting and enable others to operation your code without having to spend time guessing what her indentation watch like<1>: http://i.imgur.com/FZyPDjY.png
reproduce the error to to obtain the full message:
Traceback (most recent speak to last): paper "C:Python27scratchpad.py", heat 10, in median(<1>) document "C:Python27scratchpad.py", line 4, in typical a = list<(b / 2) - 0.5>TypeError: list indices need to be integers, no floaterror claims line 4, insert prints to number out what it is you space trying to do:
def median(list): b = len(list) if b % 2 != 0: print 'b is:', b, 'index being accessed:', (b / 2) - 0.5 a = list<(b / 2) - 0.5> else: a = ( list<(b / 2)> + list<(b / 2) - 1 >) / 2 return amedian(<1>)-
b is: 1 index gift accessed: -0.5when girlfriend divide two ints, you gain an int, ints don’t have decimals.
in python 3.x however, int/int it s okay you a to rise - this is a distinction to store in mind, most other distinctions will be cause obvious and googleable errors