Can't execute function calls

Hello. I am new to python, and this is my first app. Instead of calling paragraphbuilder() and sentencetype1(), the program flow just passes through. Thanks in advance!

""" Generate sentence type one """
def sentencetype1():
    rand = random.randint(1,2)
    if rand == 1:
        text = "I want to "
    if rand == 2:
        text = "I "
        verb_choices = random.sample(Verb, 12 + themevector1)
        text = text + random.choice(verb_choices)
        text + " the "
    if random.randint(1,2) == 1:
        adjective_choices = random.sample(Adjective, 12 + themevector1)
        text + random.choice(adjective_choices)
        noun_choices = random.sample(Noun, 12 + themevector1)
        text + random.choice(noun_choices)
    else:
        noun_choices = random.sample(Noun, 12 + themevector1)
        text + random.choice(noun_choices)
    return Adjective, Noun, Verb, text

""" Paragraph builder """
def paragraphbuilder():
    numbersentences = random.randint(1,50)
    x = 1
    while x < numbersentences:
        sentencetype = 1
        if sentencetype == 1:
            sentencetype1()
        x += 1
    return text

""" Generate story """
y = 1
while y < numberparas:
    paragraphbuilder()
    y += 1

Where is numberparas defined?

In the header declarations, before the data banks, in the first cell.

Right now you’re discarding the return value of paragraphbuilder(). Do you mean to do something with it, e.g. print it? I don’t see any print statements or other forms of output.

I am trying to use it to build text into a story.

Right. However the text that it builds isn’t being printed anywhere.

Looking a bit closer at the inside of those functions, it seems you have some other issues with return values. paragraphbuilder ends with return text, but you don’t assign to text anywhere in that function. It also calls sentencetype1, but doesn’t use the return value. Inside sentencetype1, you have return Adjective, Noun, Verb, text, where Adjective, Noun, and Verb appear to be global constants and only text seems to be needed by the caller.

You probably want to make sentencetype1 just return text, and rewrite paragraphbuilder to something like

def paragraphbuilder():
    sentences = []
    for _ in range(random.randint(1,50)):
        sentences.append(sentencetype1())
    return ". ".join(sentences)     

Be sure to print the return value of paragraphbuilder inside the main loop.

2 Likes

what do you mean : Be sure to print the return value of paragraphbuilder inside the main loop.

You should understand functions (and more general statements) either return something or have side effects. (Or both.)

eg sum returns something, whereas print has side effects.
If you call

sum(1, 2)

it executes, but it doesn’t do anything. Meanwhile

x = print(1)

is non-nonsensical.

You need to assign to the value returned by the function. Your functions are like that. They execute, but nothing happens after they execute.

BTW, try to avoid C++ - style loops.

Eg that last paragraph could be written as

numberparas = 4
""" Generate story """
story = sum(paragraphbuilder() for _ in range(numberparas))
print(story)
3 Likes

As in something like

paragraph = paragraphbuilder()
print(paragraph)

If you just call paragraphbuilder(), Python will happily construct the string then throw it away. In order to see the string, you need to capture the return value and do something with it, like call print with it, write it to a file, etc.

You mean build text with it in the main loop? How do I do that?

How do I combine this with text?

I made these changes and it had no effect

Here’s what I did for the last cell. It still doesn’t work:

""" Generate story """
y = 1
while y < numberparas:
    paragraphbuilder()
    paragraph = paragraphbuilder()
    print(paragraph)
    text + paragraph
    y += 1

Here’s what your code does:

    paragraphbuilder()

You’ve called paragraphbuilder which has returned a result, but you didn’t save that result, so it’s discarded.

    paragraph = paragraphbuilder()

You’ve called paragraphbuilder again which has returned a result, and you’ve saved that result.

    print(paragraph)

You’ve printed the result.

    text + paragraph

You’ve concatenated the text and the paragraph, but you haven’t saved the result, so it’s discarded. Presumably you meant:

    text += paragraph
1 Like
  • does the same thing

I checked it again and it works! Thanks!