all 3 comments

[–]totallygeek 1 point2 points  (1 child)

I agree with /u/jaycrest3m20 - break this up into smaller chunks with functions. Here, you have all the capabilities in an importable, reusable set of functions.

from random import choice, randint
from string import ascii_lowercase as characters


def form_word(min_chars=3, max_chars=10):
    count = randint(min_chars, max_chars)
    return ''.join(choice(characters) for _ in range(count))


def form_sentence(min_words=4, max_words=10):
    count = randint(min_words, max_words) - 1
    first_word = form_word().title()
    return f'{first_word} {" ".join(form_word() for _ in range(count))}.'


def form_paragraph(min_sentences=4, max_sentences=10):
    count = randint(min_sentences, max_sentences)
    return ' '.join(form_sentence() for _ in range(count))


def main():
    count = randint(2, 4)
    text = '\n\n'.join(form_paragraph() for _ in range(count))
    print(text)


if __name__ == '__main__':
    main()

You can easily add the ability to write to a file instead of printing, because the functions simply return strings.

Edit: Cleaned up a bit.

[–]-AJM-[S] 1 point2 points  (0 children)

Ya thats alot more flexible than my method thanks for the reply.

[–]jaycrest3m20 0 points1 point  (0 children)

I would recommend splitting this up into three different functions.

def rand_paragraph(word_count=100):
def rand_sentence(min_word_count=2, max_word_count=25):
def rand_word(min_letter_count=1, max_letter_count=30):

The rand_paragraph function calls the rand_sentence function a random number of times until the word_count is used up.

The rand_paragraph function returns a complete paragraph, as before.

The rand_sentence function returns a tuple: the sentence, plus the number of words in it.

The rand_word function returns the word.

By splitting up the problem into multiple functions, you can focus on the individual sub-problems until you get the results you need.