JumpSkitt
JumpSkitt

Reputation: 21

I'm trying to make a word generator that makes words that are non existent

The problem is that when I use this code it makes a whole new word until the word has a vowel in it instead of working with the word that it already generated. I would like it if it just changed one letter in the word that was already generated instead of generating another.

import random as r

num1 = input("How many letters does each word need to contain? ")
num2 = input("How many lines do you want to add? ")
letter = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", 
"s", "t", "u", "v", "w", "x", "y", "z"]

def start_gen(length, lines):
generated_words = open("generated_words.txt", "a")
generated_words.write(f"{length} letters per word" + f" and {lines} lines")
generated_words.write("\n")
generated_words.write("\n")
r_words = 0
valid_word = False
line = 0
on = True
while on:
    word = ""
    for i in range(0, int(length)):
        word += letter[r.randint(0, 25)]
    print(word)
    generated_words.write(word)
    generated_words.write(" ")
    if "a" in word or "i" in word or "e" in word or "o" in word or "u" in word or "y" in word:
        print(word)
        valid_word = True
    # else:
    #   word[r.randint(0, num1)] = "a"
    if valid_word:
        r_words += 1
        print(f"{r_words} Valid Words")
        valid_word = False

    if r_words == 1:
        generated_words.write("\n")
        generated_words.write("\n")
        line += 1
        print(f"{line} Lines")
        r_words = 0
    if line == lines:
        generated_words.write("----------------------------------STOP-------------------------------- 
 --")
        generated_words.write("\n")
        generated_words.close()
        print("All done")
        break


start_gen(int(num1), int(num2))

OUTPUT:

7 letters per word and 4 lines

njyjfko 
viliiuj 
bkfacce 
tqkgshk ywmzgbk 

Upvotes: 1

Views: 235

Answers (1)

Matt Miguel
Matt Miguel

Reputation: 1375

It's still not super clear to me what you are expecting, but I will interpret it as when you have a word without a vowel, you want to replace an existing consonant with a vowel. This means the maximum a single line can have is two words - one without vowels and one with a vowel substituted into a random location. If the second word is guaranteed to have a vowel and not having a vowel is the only reason you have stated for rejecting a word, then there should be at max two words in a line.

I included quite a few suggestions on making the code more readable that you can choose to incorporate if you want.

import random 
#use original module name which is already short
#single letter variables are ok if their usage is localized to one part of a script, like within one loop
#but if they are used all over the script, then descriptive (but still concise) names are better.
#An exception is if a library is pervasively used everywhere in the script like numpy or pandas
#those have conventional two letter names np and pd

from string import asciilowercase
vowels = set('aeiouy')

#move prompts to after functions are defined

#don't to type your own list of letters, can use string.asciilowercase

def start_gen(word_length, num_lines):
    #made arguments more descriptive
    #i.e length of what? - length of the word
    #number of lines - not the collection of lines itself

    #changed file handle name to out_file
    #generated_words as a variable name sounds like a list or set
    with open("generated_words.txt", "a") as out_file:
        out_file.write(f"{word_length} letters per word and {num_lines} lines\n\n")

        #too many flags that do very similar things:r_words, valid_word
        #since you know number of lines, do a for loop on that number
        for _ in range(num_lines):
            #can use random.choice to just pick a letter instead of worrying about indices. 
            #can do it in a list comprehension one-liner
            first_word=''.join([random.choice(asciilowercase) for _ in range(word_length)])
            out_file.write(first_word)
            #check if has vowels by intersection with vowel set
            if len(set(first_word)&vowels)==0:
                #no vowels
                vowel = random.choice(list(vowels))
                position = random.randint(0,word_length-1)
                second_word = first_word[:position]+vowel+first_word[position+1:]
                out_file.write(' ' +second_word+'\n\n')
            else:
                 # has vowels
                 out_file.write('\n\n')
        out_file.write("----------------------------------STOP-------------------------------- 
 --\n")
        
         

if __name__ == '__main__':
    #this check prevents the prompts from happening if this script is imported
    #Prompts will only happen if script is run directly.
    num1 = input("How many letters does each word need to contain?\n > ")
    num2 = input("How many lines do you want to add?\n > ")
    start_gen(int(num1), int(num2))

Upvotes: 1

Related Questions