Codecademy says my code is wrong but I'm getting correct return values


#1

I'm trying to figure out whats wrong with my code on the "median" exercise in the "Practice makes perfect"...
It keeps telling me I'm not getting the right median value even though the console looks like it's printing the correct number. Can anyone help? I added some extra unnecessary stuff to help me see whats going on but I didn't think any of that was interfering. Here is my code:

numbers = []
count = int(raw_input("how many numbers in your list? "))
print "input ", count, " numbers in any order to get the median number"
counter = count
for i in range(count):
counter -= 1
print "numbers left:", counter+1
numbers.append(int(raw_input()))
if counter == 0:
print "your list ", numbers

def median(numbers):
numbers.sort()
new_numbers = []
for i in numbers:
new_numbers.append(i)
print "sorted ", new_numbers
print "median: "
if count % 2 != 0:
return new_numbers[int(len(new_numbers)/2)]
else:
return ((new_numbers[int((len(new_numbers)/2)-1)]+new_numbers[int(len(new_numbers)/2)])/2.0)

print median(numbers)


#2

I don't think you are supposed to take any input. Remember, the code is machine-checked. If you expect input but the machine thinks none is required, your code won't work properly.


#3

hm. ok I'll work around that and see if it works. I've done others with input that didn't ask for it and it worked fine though.


#4

The machine enters input automatically. I ran your code in my own browser, and it tried on a 5-element list. It also asked me 4 elements, but took only one. That's an error, right there.

I suggest you remove the part with print statements and just take input of a list. That should fix it.


#5

hm no I've tried removing inputs and print statements but I keep getting the same error. Specifically it says:

"Oops, try again. median([4,5,5,4]) returned 5 instead of 4.5"


#6

When I use [4,5,5,4] as inputs I get this output:

how many numbers in your list? 4
input 4 numbers in any order to get the median number
numbers left: 4
4
numbers left: 3
5
numbers left: 2
5
numbers left: 1
4
your list [4, 5, 5, 4]
sorted [4, 4, 5, 5]
median:
4.5
None

Then followed by an error that instead reads:

"Oops, try again. median([6,8,12,2,23]) returned 7.0 instead of 8"


#7

@meahdros try this

numbers = []
count = int(raw_input("how many numbers in your list? "))
print "input ", count, " numbers in any order to get the median number"
counter = count
for i in range(count):
    counter -= 1
    print "numbers left:", counter+1
    numbers.append(int(raw_input()))
    if counter == 0:
        print "your list ", numbers

def median(numbers):
    numbers.sort()
    new_numbers = numbers
    
    '''for i in numbers:
        new_numbers.append(i)
        print "sorted ", new_numbers'''
    print "median: "
        
    if len(numbers) % 2 != 0:
        return new_numbers[int(len(new_numbers)/2)]
    else:
        return ((new_numbers[int((len(new_numbers)/2)-1)]+new_numbers[int(len(new_numbers)/2)])/2.0)

print median(numbers)

#8

that worked. Thank you very much! So it just didn't like my second empty list? I don't fully understand what the issue was...


#9

This is what I wanted to tell you. Even though you gave input as 4, 5, 5, 4, it also tried 6, 8, 12, 2, 23. The machine provides input automatically; you just have to make a provision.