5-15 question


#1

Please ignore the print lines, strictly for debugging. This works in every scenario/example I have tried, except in codecademy. For instance [4, 5, 5, 4] when I run my code it works fine and returns 4.5. When I run it in the program it generates an error "returns 4 instead of 4.5. I've tested this over and over with no issues????????


#2

on the line where you are doing

return (n[y] +n[x])/2

make it

return (n[y] +n[x])/2.0

#3

Thanks. I thought it might be a float issue, I tried converting the return to a float but your fix is simple. It's these little things that don't throw errors in real life that hang us up so often.


#4

to use floats then you would have to convert the 2 on its own to float like this

(n[y] +n[x])/float(2)

##or
float((n[y] +n[x]))/2

but if you do this

float((n[y] +n[x])/2)

You will not get the right answer because the computation would have been done as ints implicitly before it is converted to a float(i.e. you have lost some data) . For example

print float(9)/2 #4.5

#or

print 9/float(2) #4.5

#or

print float(9)/float(2) #4.5

#but
# we will loose some data before the conversion here
print float(9/2) #4.0