For the final code challenge, we are going to create a function that finds the middle item from a list of values. This will be different depending on whether there are an odd or even number of values. In the case of an odd number of elements, we want this function to return the exact middle value. If there is an even number of elements, it returns the average of the middle two elements. Here is what we need to do:

Define the function to accept one parameter for our list of numbers

Determine if the length of the list is even or odd

If the length is even, then return the average of the middle two numbers

If the length is odd, then return the middle number

Why did it return a syntax error? The default answer uses the int function. Is it possible to solve the challenge without using int at all, as I tried to do?

I can’t see the indentations in the code. Use the </> button for that.

You can subtract before dividing so that you don’t have to deal with floats like 0.5 at all. return my_list[ (len(my_list) - 1) // 2 ]
Notice the use of // for integer division in Python 3.

Yes, I know that return my_list[ len(my_list) // 2 ] would work too.

print(15 / 3)
print(15 // 3)

Note that the average of a and b would be (a + b) / 2,
but not a + b / 2