It is a code that consists of English words in the list, and then receives the word you want to find, outputs the index number from the list if there is the same thing, and outputs -1
if not.
For example, if you have ["apple"",lime"",grape"",banana"]
and type "a"
in the word you want to find, you can print it out like [0,2,3]
But you keep checking the first value of the list. And I have to make a list and add it, but it can't be added to the list. Help me.
lista=[]
def N_find(listname,string):
for i in range (len(listname)):
if listname[i].count(string)!=0:
return lista.append(i)
else:
return(-1)
num=int (input ("List element earning power:"))
NewList=[]
tempList=[0]
for i in range(num):
print(i"th")
t=input ("Enter elements to add:")
tempList=[t]
NewList=NewList+tempList
a=input ("What letters are you looking for?")
fin=N_find(NewList,a)
print(fin)
return lista.append(i) I think that's why you're only printing the first value. Try writing print() or something at the end of the function. If it is because of return, then the function is
lista=[]
def N_find(listname,string):
global lista
for i in range (len(listname)):
if listname[i].count(string)!=0:
lista.append(i)
else:
return(-1)
return lista
Do it like this
def N_find(listname, s):
ret = []
for i, word in enumerate(listname):
if word.find(s) != -1:
ret.append(i)
return ret
print(N_find(lst, 'a'))
# [0, 2, 3]
print(N_find(lst, 'b'))
# [3]
print(N_find(lst, 'e'))
# [0, 1, 2]
© 2024 OneMinuteCode. All rights reserved.