Python Forum
Why is this code wrong? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Why is this code wrong? (/thread-9382.html)



Why is this code wrong? - Lemmy - Apr-05-2018

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



RE: Why is this code wrong? - buran - Apr-05-2018

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?


RE: Why is this code wrong? - Lemmy - Apr-05-2018

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



RE: Why is this code wrong? - buran - Apr-05-2018

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


RE: Why is this code wrong? - Lemmy - Apr-05-2018

Ok i got it. Thanx