0

The full code for the program is below, it selects a random word based on the letters in the user's initial input and then is suppose to create a phrase saying, for example, x is for xylophone. The problem issue is the last function, the string variable 'phrase' is not being appended to the list 'phrases'.

from random import choice
import re
from time import sleep
from nltk.corpus import words

# This code takes user word and creates list of separate letters
user_letters = []
def get_letters():
   acrostiche = input("Enter your word here: ").upper()
   for letter in acrostiche:
      user_letters.append(letter)

# This code finds a random word for each letter in list user_letters
beginning_words = list()
user_letters2 = user_letters
def get_words_to_start_lines():
   while len(user_letters2) > 0:
      for letter in user_letters2:
         lettered_words = list(re.findall(letter + r".*", words.raw("en")))
         word = choice(lettered_words)
         lettered_words.clear
         break
      beginning_words.append(word)
      user_letters2.remove(letter)

# this code should generate a list of phrases user_letter is for beginning_word for each letter and word
phrases = list()
user_letters3 = [i for i in user_letters]
beginning_words2 = [ i for i in beginning_words]
def beginning_phrases():
   while len(user_letters3) > 0 and len(beginning_words2) > 0:
      for word in beginning_words2:
         for letter in user_letters3:
            phrase = letter + "is for" + word
      user_letters3.remove(letter)
      beginning_words2.remove(word)
      phrases.append(phrase)
   print(phrases)

get_letters()
print(user_letters)

get_words_to_start_lines()
print(beginning_words)
sleep(2)

beginning_phrases()

Here is some sample output:

Enter your word here: hi
['H', 'I']
['Hierochloe', 'Ita']
[]

The final two brackets represent the list named phrases, as you can see, nothing has been appended to it. Again, I have tried coding a simple example in a separate file which worked which I will share below.

numbers = ['1', '2', '3']
words = ['cat', 'dogs', 'mice']
phrases = []
def example():
    while len(words) > 0 and len(numbers) > 0:
        for w in words:
            for n in numbers:
                phrase = n + w
        numbers.remove(n)
        words.remove(w)
        phrases.append(phrase)
    print(phrases)

print(numbers)
print(words)
example()

The sample output for this is as such:

['1', '2', '3']
['cat', 'dogs', 'mice']
['3mice', '2dogs', '1cat']
Barmar
  • 741,623
  • 53
  • 500
  • 612
Hermit
  • 3
  • 3
  • `user_letters2.remove(letter)` is removing the letters from `user_letters`. `user_letters2` is not a copy, it's the same list. – Barmar Jun 09 '21 at 18:41
  • I have tried making two copies of the user_letters list using two methods: user_letters2 = [i for i in user_letters] and user_letters3 = user_letters.copy() but these both return blank lists too. – Hermit Jun 09 '21 at 18:58
  • It doesn't matter, but `lettered_words.clear` doesn't do anything, since it's missing `()`. But there's no need to clear the list between iterations. – Barmar Jun 09 '21 at 19:04
  • Thanks for your help, I managed to sort it by creating a copy of the list within each function rather than outside them and thanks for the heads up on the typo. I appreciate it. – Hermit Jun 09 '21 at 19:06
  • You're setting `user_letters3` and `beginning_words2` before you call the functions that fill in `user_letters` and `beginning_words`, so you're making copies of empty lists. – Barmar Jun 09 '21 at 19:10
  • Thank you, that makes a lot of sense. You've been quite helpful. – Hermit Jun 09 '21 at 19:17

1 Answers1

0

In addition to the answer above, the line user_letters2.remove(letter) is outside the scope of the for loop that defines letter.

RobJarvis
  • 360
  • 3
  • 8