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 what
s 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])
<do not remove the three backticks above>