Please help

I don’t know what is the mistake here. please tell me the reason

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

#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))

here:

return lst[int(m/2)]

you cast to integer because the result of the division is a float. So not sure why you don’t do that here:

return (lst[(m/2) -1] + lst[m/2])/2

i had to assume you work in python3? Can you share the exercise url?

1 Like

I thought m%2 == 0 so m/2 would be integer
can u explain this please

division behavior is determined by the programming language, if you do:

print(6/2)

you see that the result a float

you could consider using floor division.

2 Likes

ttttttttttthank you :]

You can also use //, which performs integer division, so 6 // 2 is 3 rather than 3.0. Then you can eliminate using the int function.

1 Like

which is called floor division :wink:

def delete_starting_evens(lst):
for num in lst:
if lst[0]%2 == 0:
del lst[0]
return lst

print(delete_starting_evens([56, 98, 66]))

answer is not empty set? why [66]

Removing items from a list where you are looping over is a recipe for disaster. Items shift position, the list shortens.

1 Like

can you explain more? please explain how my code work to get 66

The easiest way to understand that is to it through a debugger:

http://www.pythontutor.com/visualize.html#mode=edit

so you can see each step

1 Like

wtf is this jesus? thank you so much :smile:

I think that it’s Jeremiah:

Call unto me, and I will answer thee, and show thee great and mighty things, which thou knowest not.