select one of the words contained in the list

Asked

Viewed 177 times

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.

  • 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.

  • 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)

  • 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.')

  • obg for the interest in helping.

  • Then why the append and remove if you just want to check if the element exists in the list ? To do this the ideal is to use the operator in as already shown in the answer you have at the moment. Something as simple as if palavra in lista:

  • 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.

  • the append is so that the words typed during the execution of the program are placed in the list.

Show 3 more comments

1 answer

2


You are with a useless variable i.

for palavra in lista:
    if palavra == buscada:
        print('a palavra buscada foi encontrada na lista')

Another way would be:

if buscada in lista:
    print('palavra encontrada')

I believe you might have found the answer to your question on some other topic.

  • Thanks for replying Ken. But where would the "for" fit? inside the while loop or outside?

  • Thank you for helping me find the answer

  • list = [] i = 0 word = ' searched = ' while (word.Lower() != 'exit'): 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: .')

  • Good! The two forms are valid. It is worth remembering that in most cases the second form will be more performative, why the for will scroll through the entire list even after finding the word. Already with the in, it will stop iteration as soon as it finds the searched element.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.