0
Type a word in a list and then search for the typed word
lista = []
i = 0
palavra = ''
while (palavra.lower() != 'sair'):
i += 1
palavra = str(input('digite alguma palavra: '))
lista.append(palavra)
ultima_palavra = len(lista) - 1
lista.remove(lista[ultima_palavra])
print(lista)
What is the python function that looks for strings in a list? can’t be a specific word, because the program allows you to type any word and then search between what was typed.
– Homero
But what are you trying to do? Explain in words how you idealize the user’s interaction with the program and its functioning, because the code you have does not play with the question title.
– Isac
for example I type: blue, day, night then I type "exit" then the option appears for the user to type a word, if it appears in the list will appear a sentence telling him that there is such a word in the list, if not he will receive information that the word is not in the list (in the example quoted "red" would not be in the list and blue yes)
– Homero
list = [] i = 0 word = ' searched = ' while (word.Lower() != 'quit'): i += 1 word = str(input('type some word: ')) list.append(word) ultima_word = Len(list) - 1 list.remove(list[ultima_word]) print(list) searched = str(input('type the searched word: ') if searched in list: print('the word', searched, 'searched was found in the list) Else: print('word not found.')
– Homero
obg for the interest in helping.
– Homero
Then why the
append
andremove
if you just want to check if the element exists in the list ? To do this the ideal is to use the operatorin
as already shown in the answer you have at the moment. Something as simple asif palavra in lista:
– Isac
because if the word "exit" does not appear at the end of the list. , and since it is necessary for the program to stop the loop, I used the option to remove it.
– Homero
the append is so that the words typed during the execution of the program are placed in the list.
– Homero