Python Forum
Why is this code wrong?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this code wrong?
#1
This is the quiz:

The function in this quiz, median, returns the median value of an input list. Unfortunately it only works with lists that have an odd number of elements. Modify the function so that when median is given a list with an even number of elements, it returns the mean of the two central elements. The provided test cases demonstrate the expected behavior.

def median(numbers):
    numbers.sort() #The sort method sorts a list directly, rather than returning a new sorted list
    middle_index = int(len(numbers)/2)
    return numbers[middle_index]

test1 = median([1,2,3])
print("expected result: 2, actual result: {}".format(test1))

test2 = median([1,2,3,4])
print("expected result: 2.5, actual result: {}".format(test2))

test3 = median([53, 12, 65, 7, 420, 317, 88])
print("expected result: 65, actual result: {}".format(test3))
That's my answer:
def median(numbers):
    numbers.sort() 
    if len(numbers)%2: 
        middle_index = int(len(numbers)/2)
        return numbers[middle_index]
    else:
        middle2 = len(numbers)/2
        return (numbers[middle2]+numbers[middle2-1])/2
And this is the error that i get:
Error:
Traceback (most recent call last): File "vm_main3.py", line 47, in <module> import main File "/tmp/vmuser_uohknylyuz/main.py", line 2, in <module> import studentMain File "/tmp/vmuser_uohknylyuz/studentMain.py", line 1, in <module> import median File "/tmp/vmuser_uohknylyuz/median.py", line 13, in <module> test2 = median([1,2,3,4]) File "/tmp/vmuser_uohknylyuz/median.py", line 8, in median return (numbers[middle2]+numbers[middle2-1])/2 TypeError: list indices must be integers, not float expected result: 2, actual result: 2
Reply
#2
well, it fails test with list with 4 elements. So according to test expected result is 2.5, not 2 as shown in your traceback.
I think the problem is that the code is intended for python3 and you run it with python2.
In python2 / is floor devision, so 5/2 = 2 and expected answer is 2.5
Hm, that is strange. My previous post is not correct
1. According to traceback, the error is with test2
2. however the TypeError says the list index is float, i.e. len(numbers)/2 is float, which suggest the len is odd number (3?), not even (4). This is consistent with expected result 2.
Are you sure this is correct traceback produced from this code?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
This is the correct answer:
def median(numbers):
    numbers.sort() 
    if len(numbers) % 2:
        # if the list has an odd number of elements,
        # the median is the middle element
        middle_index = int(len(numbers)/2)
        return numbers[middle_index]
    else:
        # if the list has an even number of elements,
        # the median is the average of the middle two elements
        right_of_middle = len(numbers)//2 
        left_of_middle = right_of_middle - 1
        return (numbers[right_of_middle] + numbers[left_of_middle])/2

test1 = median([1,2,3])
print("expected result: 2, actual result: {}".format(test1))

test2 = median([1,2,3,4])
print("expected result: 2.5, actual result: {}".format(test2))

test3 = median([53, 12, 65, 7, 420, 317, 88])
print("expected result: 65, actual result: {}".format(test3))
It produces this result:
Output:
expected result: 2, actual result: 2 expected result: 2.5, actual result: 2.5 expected result: 65, actual result: 65

When i run my code i get:
Error:
Traceback (most recent call last): File "vm_main3.py", line 47, in <module> import main File "/tmp/vmuser_wnvdpodgau/main.py", line 2, in <module> import studentMain File "/tmp/vmuser_wnvdpodgau/studentMain.py", line 1, in <module> import median File "/tmp/vmuser_wnvdpodgau/median.py", line 13, in <module> test2 = median([1,2,3,4]) File "/tmp/vmuser_wnvdpodgau/median.py", line 8, in median return (numbers[middle2]+numbers[middle2-1])/2 TypeError: list indices must be integers, not float expected result: 2, actual result: 2
Reply
#4
OK, the problem with your code - in python3 / will produce float, so 4/2 = 2.0 and it expects int
compare it with the correct answer from your second post. there they use // which in python 3 is floor division, so 4//2 = 2 (which is int, and not 2.0 which is float)
in your code you can change line7 to
middle2 = int(len(numbers)/2)
or use // to get floor division as in the 'correct' answer
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Ok i got it. Thanx
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  im not sure what ive done wrong code doesnt run dgizzly 3 1,385 Nov-16-2022, 03:02 AM
Last Post: deanhystad
  what is wrong with my code 53535 4 1,570 Apr-07-2022, 11:37 AM
Last Post: 53535
  What's wrong with my code? NeedHelpPython 4 2,247 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it? shirleylam852 1 2,687 Dec-09-2020, 06:37 AM
Last Post: stranac
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 4,717 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Something is Wrong with my code susmith552 4 3,053 Nov-28-2019, 02:16 AM
Last Post: susmith552
  What is wrong with my code? Than999 1 2,392 Nov-10-2019, 08:59 PM
Last Post: ichabod801
  Wrong output on my code. JTNA 2 7,931 Apr-04-2019, 01:55 PM
Last Post: JTNA
  What's wrong with my code and visuals for python? beginnercoder04 2 2,828 Mar-17-2018, 01:06 AM
Last Post: beginnercoder04
  whats wrong with my code? syntax errors r6lay 5 6,511 Mar-16-2017, 04:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020