Advanced List Challenge #5 Middle Item

hello everyone, I’m trying to solve challenge #5 where you are supposed to return the middle element of a list if there is an odd number of elements or the average of the two middle elements if it is even.
Here is my code:

#Write your function here
def middle_element(lst):
if len(lst) % 2 != 0:
mid = (len(lst) - 1) / 2
return lst[int(mid)]
m1 = ((len(lst) / 2) - 1
m2 = ((len(lst) / 2)
return (m1 + m2) / 2

print(middle_element([5, 2, -10, -4, 4, 5]))

For some reason I am getting a syntax error for the variable titled m2. Anyone know why I am receiving this error?

is your code properly formatted (read: indented)? Also, your function should take 3 parameters, lst, start, end

Check out these threads here.

Yes the indentations are correct I believe! When I copied and pasted it did not format correctly.

And the challenge states to only take one parameter named lst.

These are the instructions:

"Our next function will remove all elements from a list with an index within a certain range. The function will accept a list, a starting index, and an ending index. All elements with an index between the starting and ending index should be removed from the list. Here are the steps:

  1. Define the function to accept three parameters: the list, the starting index, and the ending index
  2. Get all elements before the starting index
  3. Get all elements after the ending index
  4. Combine the two partial lists into the result
  5. Return the result"

Think about indexes here. You don’t even need to write a for loop either.

Wrong challenge.

  1. Define the function to accept one parameter for our list of numbers
  2. Determine if the length of the list is even or odd
  3. If the length is even, then return the average of the middle two numbers
  4. If the length is odd, then return the middle number

Correct instructions

I am human. I make mistakes. I didn’t scroll down far enough.

Double check how you use %

All good, just letting you know :slight_smile:


I fixed your variable to no longer give a syntax error, but your code doesn’t return the proper value… here is the new variables

m1 = lst[int(len(lst)/2)] - 1
m2 = lst[int(len(lst)/2)]

should fix your syntax error

@griffineast1 Talk out each bit of code and what it’s doing. that’s how one can debug code…

I gave him the updated variables because even the documentation for syntax error says its uninformative.

He still needs to write his code to output the correct answer. The updated values are nothing more or less than can be found in the drop-down help tip,

short answer to the original question:For some reason I am getting a syntax error for the variable titled m2. Anyone know why I am receiving this error?
there’s an extra parenthesis there
m1 = (len(lst) / 2) - 1
m2 = (len(lst) / 2)

Anyone know why I am receiving this error?

Because there’s an extra left parenthesis, Python is expecting a continuation in the next line and this is fine.
Instead, it encounters an assignment statement to m2 and this is not correct, hence reports syntax error.
On some IDE, it clearly indicates the error location in m2:

    m2 = ((len(lst) / 2)
SyntaxError: invalid syntax

Besides, hope you have reviewed your code. What you’re doing is taking the value of the length directly instead of using it as an index to the list. This was mentioned by @bitninja14517 in earlier reply.

Just like @tezza_chen said, you had to many (. Read the directions and look at the help tip. You want to use the indices to solve this one.