NameError: name '' is not defined

Hello. I am learning Python, and this is my first app. I am getting this error: NameError: name ‘Verb’ is not defined. Massive thanks in advance for any help!

""" Generate sentence type one """
def sentencetype1(Verb, Adjective, Noun):
    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 text
""" Paragraph builder """
def paragraphbuilder():
    numbersentences = random.randint(1,50)
    x = 1
    while x < numbersentences:
        sentencetype = 1
        if sentencetype == 1:
            sentencetype1(Verb, Adjective, Noun)
        x += 1
    return
""" Generate story """
y = 1
while y < numberparas:
    paragraphbuilder()
    y += 1

You haven’t posted the traceback, so I’m going to assume that it occurred on this line:

            sentencetype1(Verb, Adjective, Noun)

The name Verb exists as a parameter of sentencetype1, so it’s known only in that function.

NameError Traceback (most recent call last)
Cell In[10], line 4
2 y = 1
3 while y < numberparas:
----> 4 paragraphbuilder()
5 y += 1

Cell In[9], line 8, in paragraphbuilder()
6 sentencetype = 1
7 if sentencetype == 1:
----> 8 sentencetype1(Verb, Adjective, Noun)
9 x += 1
10 return

NameError: name ‘Verb’ is not defined

What should I do to fix it?

The function paragraphbuilder passes the value of Verb to sentencetype1, but what is its value? Nowhere in that code do you assign a value to Verb.

I assign verb as a list.

Adjective, verb and noun are lists. I got rid of the error, by moving them to the top, but now it’s not buliding text with sentencetype1.

You will need to show your current code, since you have changed it. Also the output of the run (error message or whatever).

Not in the code you posted, as far as I can see.

In you code you define sentencetype1 like this:

 def sentencetype1(Verb, Adjective, Noun):

The names Verb etc are known inside this function only. They receive
their values from the arguments you supplied when you call sentencetype1.

If you also define Verb outside the function, that is an unrelated
variable. They’re different things.

In paragraphbuilder you call sentencetype1(Verb, Adjective, Noun),
but these variables are not known when you issue the call.

 def paragraphbuilder():
     numbersentences = random.randint(1,50)
     x = 1
     while x < numbersentences:
         sentencetype = 1
         if sentencetype == 1:
             sentencetype1(Verb, Adjective, Noun)

Variable scope in Python functions is iferred from how they are used. If
you assign to a variable in a function, that is a local variable. For
example, x is local to this function and unknown outside it. If you
only access a variable, that does not make a local variable and Python
looks for it in the outer scope i.e. the module or “global” scope. So
that’s where Python looks for Verb, and it isn’t defined.

You mentioned that you’ve modified the code to define Verb and friends
at the top of the script. That will putthem in the module (aka “global”)
scope, and now they can be found.

Minor unrelated remark: function docstrings are part of the function, so
you want to define them like this:

 def paragraphbuilder():
     """ Paragraph builder """

i.e as the first expression inside the function.

1 Like

So how do I fix it?