I am looping through a list of booleans my conditions are if the position of the element is > m and if the element = True
the function will return the position of the element
this is what I've done :
panda =[True, True, True, True]
def find_next (l, m):
for i in l:
if ((l.index(i) > m) and i ==True):
return l.index(i)
print(find_next(panda, 2))
I expected the output to be 3.
but I got None
. why?
l.index(i)
always returns 0 because it's finding the first instance of True
in your list
As an aside, you don't need to call l.index
on every loop because you are redundantly searching the list when you should already know what iteration you are on.
def find_next(l, m):
for index, value in enumerate(l):
if index > m and value:
return index
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.