Syntax error I can't understand

Hello. I am learning Python, and when I try to run this I get a syntax error: invalid syntax error, and I have no idea why. Any help is massively appreciated! Thanks!

Python’s syntax error are not always perfect in the place they point to. You should also always check the previous lines. Here the syntax highlighting should give you a hint: Else is not colored as a keyword.

This is because the correct keyword is else, upper vs lower case is important in python.

2 Likes

I changed the else to lower case and still get the same error

Like @MegaIng says, when you encounter a syntax error that doesn’t make sense, check the previous lines. In this case, you have an unmatched square backet [ on the previous line (the line beginning with text += Verb[…). Python doesn’t complain until the next line since a multiline expression inside of [] is perfectly ok (so the missing ] is not an error), but an assignment operator like += inside of an expression is not ok (so encountering text += … on line 9 is an error).

Note that you have a few other typos as well, like trailing commas that will create unexpected tuples.

In the future, it’s best to post code as formatted text instead of posting screenshots.

P.S. instead of generating list indices with random.randint, consider using random.choice.

2 Likes

You’re right I had a bunch of unclosed brackets.

Hi,

another error that you are going to run into is that the keyword Verb is not defined. From which library package is it being imported / referenced from?

Additionally, you also can’t add an int type to a str type. In order to do so, you have to make use of type conversion.

import random

text = ""  # of type str
themevector1 = random.randint(0, 108)  # of type int
print('The value of themevector1 is:', themevector1)
print('The variable themevector1 is of type: ', type(themevector1))
print('The variable text is of type: ', type(text))

# Note that text variable is type string (str).  You want to add integer type to type string
# You have to implement type conversion in order for it work
text += str(themevector1) + str(random.randint(0, 11))

print(text)

Verb is an array of words. I am trying to mathmatically create language.

Where are the trailing commas?

I tried random.choice but it had errors, now I am still getting errors for it even though I changed back to randint.

To pick random words, your code uses expressions of the form

constant = random.randint(0, A)
# ...
word_collection[constant + random.randint(0, B)]

This generates a random word in two steps: first generating a random integer, and then indexing a list using that integer. This has the drawback that you have two hardcoded constants (A and B) that need to be kept in sync with the length of each word_collection list (in your code, Noun, Verb, Adjective). If anything falls out of sync, you risk getting an opaque IndexError at runtime.

A safer (and often more readable) way to do that sort of random item generation is to bypass integers and list indexing and instead use the functions from the random module that accept sequence containers as their arguments, like random.choice.

I’m guessing the constant offsets in your code (e.g. themevector1) are designed to mix-up/rotate which subset or the total word bank is being used on any given run. That being the case, an alternative way to do this might looking something like this:

# All possible verbs.
verb_bank = [...]
# Pick only among a random subset of 11 verbs during this run.
verb_choices = random.sample(verb_bank, 11)
# ...
text += random.choice(verb_choices)

Note that there’s no risk of things falling out of sync or picking invalid indices.

For example:

text = "I ",

and

text += Verb[themevector1 + random.randint(0,11), # sic: missing ]
2 Likes

I got this with random.choice. I’m still getting it even though I changed to random.int.
TypeError: Random.choice() takes 2 positional arguments but 3 were given

I am now getting an error for something that is no longer there. I think I have a broke app.

You could post the code and traceback so that we can tell you where you’re going wrong.

Please don’t post screenshots.

Copy and paste any code or traceback, and in order to preserve formatting, select the code or traceback that you posted and then click the </> button.

1 Like

I switched it to random.choice and got it to work, once I put in the right number of arguments. The answer was in his reply, I just needed to read it.

What version of Python are you using? In at least 3.11+, one should get a more helpful message:

>>> [1,
...  a += 3
...  

SyntaxError: '[' was never closed

Note: IDLE highlights the [ in red. The Else before that should have been caught.

1 Like

Python 3. It gave me error messages for code I had removed.

Typically that means that you’d removed the code but not yet saved the
file, so Python sees the old code because the file still has the old
code.

I’ll remember that. Thanks

‘version’ means 3.8 versus, for instance, 3.13.

In the upper left it says Python 3