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']