Median 15/15


#1

Hello, I created the code below but idk why it returns 4 instead of 4.5. Can someone help me please?

import math

def median(numbers):
numbers = sorted(numbers)
if numbers == []:
return []
elif len(numbers) == 1:
return numbers[0]
else:
for num in numbers:
if len(numbers) % 2 != 0:
num = numbers[math.floor(len(numbers) / 2)]
elif len(numbers) % 2 == 0:
rnum = numbers[len(numbers) / 2]
lnum = numbers[(len(numbers) / 2) - 1]
num = (lnum + rnum) / 2
return num


#2

if the division involves two integers, python will return an integer, it will floor the result if needed (floor = round down to the nearest integer)


#3

OK thank you I figured it out :clap:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.