15 Median works on CC but not in "Real life"


#1

Hey guys..i solved the taks but whenever try that in another program it gives me the following error message:

"C:\Python 3.5\python.exe" "G:/Python/python selbstversuche/Übung - Medianberechnung.py"
Traceback (most recent call last):
File "G:/Python/python selbstversuche/Übung - Medianberechnung.py", line 11, in
median([1, 9, 5, 3])
File "G:/Python/python selbstversuche/Übung - Medianberechnung.py", line 7, in median
return (ls_sortiert[v_index - 1] + ls_sortiert[v_index]) / 2.0
TypeError: list indices must be integers or slices, not float

Doesnt really make sense if it works on CA but not in a real program..Can u tell me how to fix it and explain whats happening pls?


def median(sequence):
    ls_sortiert = sorted(sequence)
    v_laenge = len(ls_sortiert)
    v_index = v_laenge / 2

    if v_laenge % 2 == 0:
        return (ls_sortiert[v_index - 1] + ls_sortiert[v_index]) / 2.0
    else:
        return ls_sortiert[v_index]

median([1, 9, 5, 3])


#2

@rizzo36,
Try it by changing

v_index = int(v_laenge / 2.0)

You are running your code in version 3.5 and codecademy is running....

from platform import python_version
print python_version()

To find difference or discussions
== discussions / opinions ==
python [your-question] site:stackoverflow.com