Syntax error I can't understand

The version is the first part the response to this:

>>> import sys; sys.version
'3.13.0rc1 (tags/v3.13.0rc1:e4a3e78, Jul 31 2024, 20:58:38) [MSC v.1940 64 bit (AMD64)]'

Any environment should display this somewhere so one knows which version of Python it can run.

1 Like

I did this exactly like you said, and got this error:

Cell In[2], line 3
text = text += random.choice(verb_choices)“”
^
SyntaxError: invalid syntax

Can you provide the full cell (not just line 3) as formatted text?

Are those quotes at the end part of your code or a copy-paste error?

text = some_expression evaluates some_expression and assigns the result to text.

text += some_expression evaluates some_expression and adds the result to text.

You have text = text += some_expression. What is it supposed to do?

It’s part of the error message.

“”" 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 += 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 += Noun[themevector1 + random.choice(12)]
return text

Build the gen ai output

Can you please format your code by surrounding it with triple backticks (```)? We can’t tell what it’s supposed to be without proper indentation.

What I think @MRAB was asking is what you expect a statement of the form a = a += b to do. That’s what it looks you you wrote in the original exception message (though it’s hard to be sure without proper formatting). Curiously there doesn’t seem to be a line matching that in the second code snippet you posted, so I’d recommend double checking that you properly saved the file / re-ran any previous cells after editing.

Sorry.


""" 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 += 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 += Noun[themevector1 + random.choice(12)]
    return text

It doesn’t look like that code will produce the exception you got.

Do you get the same error if you re-run the cell? Be sure to re-run any cells that define functions you’ve edited. If the error persists, can you post the full exception trace back as formatted text?

If I rerun the cell by itself, nothing happens–no error