Home > Net >  python i need to not repeated words in a while loop
python i need to not repeated words in a while loop

Time:01-14

I need to not repeat words in this searcher thi this is the problem it shouldnt repeated words because the words have been already used

i have this, i tried to used a pop, a list.remove but it doesnt work or at least i didnt get results

import random

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
c = True
while c:
    c = input("")
    filtered_list = []
    for word in a:
      if c.upper() in word:
        filtered_list.append(word)

    if len(filtered_list) > 0:
        words = random.choice(filtered_list)
        print(words)
    else:
        print(f"there are any words with {c} or all the words have been used")```

CodePudding user response:

i'm not sure the original message of the example to solve; but i tried this

#!/usr/bin/python3
import random

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
c = True
while c:
    c = input("")
    print("a ", a)
    filtered_list = []
    for word in a:
        if c.lower() in word:
            a.remove(word)
            filtered_list.append(word)

    if len(filtered_list) > 0:
        words = random.choice(filtered_list)
        print(words)
    else:
        print(f"there are any words with {c} or all the words have been used")

CodePudding user response:

Use another list for used words:

from random import choice

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]

used = []
while c := input('').lower():
    filtered_list = [word for word in a if c in word and word not in used]
    if filtered_list: used  = [words := choice(filtered_list)]; print(words)
    else: print(f'there are any words with {c} or all the words have been used'); # used = []

Output:

e
ocean
e
blondie
e
threw
e
dime
e
theme
e
hello
e
there are any words with e or all the words have been used

You might also need to reset used list to empty list after reach else statement for different search input of another character or letter.

CodePudding user response:

You should start by creating a new normalised list because this code is destructive. In the question, the list contains all lowercase words but it's better to generalise

Then:

from random import choice

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]

acopy = [word.lower() for word in a]

while acopy:
    if (c := input('Enter a single character: ').lower()) == '?':
        print(acopy)
    elif len(c) == 1:
        if possibles := [i for i, word in enumerate(acopy) if c in word]:
            print(acopy.pop(choice(possibles)))
        else:
            print(f'There are no words containing {c}')

As written, the while loop will only terminate when the acopy list is empty. You might want to do something else such as empty input.

Bonus: Enter ? to see what's remaining in the list

CodePudding user response:

If i understand your problem correctly, your requirement is once the word displayed, it will not displayed again. So delete the word from the list a once you get the value from random.choice , so that you won't get the word again or maintain another list which holds the value already shown

import random

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
already_show = []
c = True
while c:
    c = input("Enter Letter to search:")
    filtered_list = []
    for word in a:
      if c in word:
        filtered_list.append(word)

    if len(filtered_list) > 0:
        words = random.choice(filtered_list)
        if words not in already_show:
            already_show.append(words)
            print(words)
        else:
           print("random.choice selected the already selected word")
        already_show = [] #Reset for another search
    else:
       print(f"there are any words with {c} or all the words have been used")
  • Related