# Can't execute function calls

**URL:** <https://discuss.python.org/t/cant-execute-function-calls/61070>\
**Category:** Python Help\
**Created:** [August 16, 2024, 7:31pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070 "2024-08-16T19:31:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 7:31pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/1 "2024-08-16T19:31:34Z")

</div>

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!

```python
""" 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

```

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [August 16, 2024, 7:34pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/2 "2024-08-16T19:34:40Z")

</div>

> [@George2](#):
>
> ```python
> y = 1
> while y < numberparas:
> paragraphbuilder()
> y += 1
> 
> ```

Where is `numberparas` defined?

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 7:36pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/3 "2024-08-16T19:36:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [August 16, 2024, 7:39pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/4 "2024-08-16T19:39:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 7:41pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/5 "2024-08-16T19:41:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [August 16, 2024, 7:53pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/6 "2024-08-16T19:53:30Z")

</div>

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

```python
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.

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 7:58pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/7 "2024-08-16T19:58:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![petercordia](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@petercordia](https://discuss.python.org/u/petercordia)\
**Post date:** [August 16, 2024, 7:58pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/8 "2024-08-16T19:58:53Z")

</div>

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

```python
sum(1, 2)

```

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

```python
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

```python
numberparas = 4
""" Generate story """
story = sum(paragraphbuilder() for _ in range(numberparas))
print(story)

```

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [August 16, 2024, 8:04pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/9 "2024-08-16T20:04:23Z")

</div>

> [@George2](#):
>
> what do you mean : Be sure to print the return value of `paragraphbuilder` inside the main loop.

As in something like

```python
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.

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 8:09pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/11 "2024-08-16T20:09:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 8:10pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/12 "2024-08-16T20:10:41Z")

</div>

How do I combine this with text?

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 8:24pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/13 "2024-08-16T20:24:07Z")

</div>

> [@George2](#):
>
> ```python
> """ Generate story """
> y = 1
> while y < numberparas:
> paragraphbuilder()
> y += 1
> 
> ```

I made these changes and it had no effect

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 9:04pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/14 "2024-08-16T21:04:50Z")

</div>

> [@bschubert](#):
>
> ```python
> paragraph = paragraphbuilder()
> print(paragraph)
> 
> ```

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

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

```

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [August 16, 2024, 9:19pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/15 "2024-08-16T21:19:10Z")

</div>

Here’s what your code does:

```python
    paragraphbuilder()

```

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

```python
    paragraph = paragraphbuilder()

```

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

```python
    print(paragraph)

```

You’ve printed the result.

```python
    text + paragraph

```

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

```python
    text += paragraph

```

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 10:01pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/16 "2024-08-16T22:01:22Z")

</div>

- does the same thing

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://discuss.python.org/u/George2)\
**Post date:** [August 16, 2024, 10:10pm UTC](https://discuss.python.org/t/cant-execute-function-calls/61070/17 "2024-08-16T22:10:56Z")

</div>

I checked it again and it works! Thanks!
