HELP - Phyton Paragraph Generator

Once again, I am asking for a bit of help. I am trying to write this following program.

Program Guideline/Description:
Write a program that is capable of generating random sentences (repeatedly) using lists and strings. Start by reviewing what components make up a sentence and the order in which they occur e.g. subject, verb, object
You may also want to do some research on “Noam Chomsky Syntactic Structures.”
*Make a plan (either as point-form pseudo code or as a flowchart) for how you will randomly pick words from lists of articles, nouns, verbs (transitive, intransitive), adjectives, adverbs etc. and generate a sentence patter. Do apply the appropriate capitalization and punctuation. *
Your program should be able to generate various sentences.

I am trying to get a top mark, which includes this: * 50 marks: generate a random paragraph (repeatedly) consisting of at least 5-10 sentences of different lengths and of different variety / types by combining words from lists of nouns, adjectives, verbs, adverbs, conjunctions* etc. Each time the program loops, a new paragraph should be generated. At least two sentences contain 6 or more words . Appropriate punctuation and capitalization are applied correctly.

And this is what I have so far.

It keeps repeating the same random sentence, and I genuinely cannot for the life of me figure out how to generate 5-10 random sentences of varying length. Would someone be able to help me out? I also need to include a function somehow.

Hi Keira,

Sorry, I cannot see your screenshot. All I see is something that looks
similar to:

[Screenshot (131)|690x312](upload/pWgQlzNz057id345YFP3FMypj0.png)

so I have to guess what you are doing.

Unless you use Photoshop to edit your code, it is better to include it
as text in the body of your post (formatted as code please) rather than
as an image.

My guess is that you have something like:

# Prepare a random sentence
sentence = ...  # fill in the details

And then you decide to generate more sentences:

while True:
    answer = input("Print another sentence? Y/N ")
    if answer == 'N':

but every time you loop, you get the same sentence. Am I close? If I am
close, that’s because you never generate a new sentence, you just
print the same sentence over and over again. There is nothing in the
loop that gives you a new sentence!

you can fix this by putting the sentence details in a function:

def make_sentence():
    s = ...  # fill in the details

sentence = make_sentence()

while True:
    answer = input("Print another sentence? Y/N ")
    if answer == 'Y':
        sentence = make_sentence()
    elif answer == 'N':

Does that help?

Obviously your code is not going to be the same as my guess, but if my
guess is close enough, it should give you some ideas for how to proceed.
Good luck!

Hey @KPhyton , as I’m not stuck viewing plain-text email :smirk: like someone here, I can see your image just fine (though regretfully, he and his email-bound brethren do have a distinct advantage when viewing code that isn’t formatted as a code block). All jokes aside, you very much should always paste code as code, like this:


In any case, I can confirm Steven’s guess is unerringly accurate. To elaborate, your code only generates sentanceOne once, on line 15, and your main while loop only prints it over and over, exactly as Steven presciently inferred. Instead, as he suggests, you could generate it inside a function and call that function in the loop.

There’s also some other things to note here. You have a while loop that picks choices for each component and assigns them to variables, but the loop always breaks after the first iteration, so it is as if the while loop was not there at all. Furthermore, the variables are never used anywhere, so that entire block has no obvious effect (did you mean to use them to construct the sentence)?

It is particularly unclear what they are even supposed to do, as they are named by sequential alphabetic letters, which is very difficult to understand remember which corresponds to what. Perhaps name them the singular form of the plural names of each of your lists instead? Finally, further highlighting the issues with such names, a is also defined to mean something else at the beginning of your program (and then, as far as I can tell, never used either), which is then overwritten by the other a in the same scope meaning something else. This creates a serious risk of bugs and confusion, so make sure to always give your variables unique and descriptive names.

On a UI/UX note, when asking the user whether they want to create a paragraph or not, why give them yes and no options, but then map those options to numbers, ask them to enter them, convert them to an integer, and then simply check which one is passed? Instead of doing all that, its simply for both you and the user to just ask them directly what choice they want, and have them type it. For example, you could ask "Do you want to generate a paragraph? Type "yes" or "no"? and have the check be something like if choice.lowercase().strip() in ("yes", "y"), etc (which converts their input to lowercase, strips white space and then checks if it is “yes” or “y”, so that Yes , yes, Y and y all match.

Finally, your variables are generally named well, but stay consistent—Python generally uses snake_case for variables and plurals for collections (like lists), as you do, but sentanceOne mixes in camelCase which generally isn’t used. Its also a good idea to use UPPER_SNAKE_CASE for constants, like your word lists.

Best of luck!