0
def median(sequence):
  sort = sorted(sequence)
  print(sort)
  if len(sequence)%2 ==0:
    print ((sort[int(len(sequence)/2-1)]+sort[int(len(sequence)/2)])/2)
  elif len(sequence)==1:
    return sort[0]
  else:
    return sort[int(len(sequence)/2)+1]


median([4,5,5,4])

This is part of a codecademy course. I am just a beginner in python. In codecademy, my code returned 4 instead of 4.5 However, when I input this code into pyscripter, it returned 4.5 which is the correct answer.

Is there any issues within my program, or is it just an issue of the compiler?

sven tan
  • 149
  • 3
  • 14
  • 2
    Looks like Python-2.x versus Python-3.x since in Python-2.x `x/y` is integer division in case the operands are integers. – Willem Van Onsem Dec 17 '17 at 15:00
  • A better duplicate (although "no accepted answer") is https://stackoverflow.com/q/29707906/2564301, where OP was similarly bamboozled. – Jongware Dec 17 '17 at 15:06
  • 1
    Wait how is it a duplicate of the question brought up by Daniel – sven tan Dec 17 '17 at 15:08
  • The question is different but the top answer explains it. But read the top rated (and sadly not accepted) answer to my link above. – Jongware Dec 17 '17 at 15:32

0 Answers0