# Syntax error I can't understand

**URL:** <https://discuss.python.org/t/syntax-error-i-cant-understand/60051>\
**Category:** Python Help\
**Created:** [August 5, 2024, 8:52pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051 "2024-08-05T20:52:10Z")\
**Posts on this page:** 20\
**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 5, 2024, 8:52pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/1 "2024-08-05T20:52:11Z")

</div>

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!

 ![Screenshot (2)](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/7/3/732e641aebd5da2d2aa6f5ac215b8cfa97af80fa.png)

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [August 5, 2024, 8:56pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/2 "2024-08-05T20:56:15Z")

</div>

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.

---

<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 5, 2024, 9:06pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/3 "2024-08-05T21:06:25Z")

</div>

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

---

<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 5, 2024, 9:09pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/4 "2024-08-05T21:09:56Z")

</div>

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](https://discuss.python.org/t/about-the-python-help-category/42) instead of posting screenshots.

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

---

<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 5, 2024, 9:15pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/5 "2024-08-05T21:15:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [August 5, 2024, 9:20pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/6 "2024-08-05T21:20:52Z")

</div>

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.

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

```

---

<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 5, 2024, 9:35pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/7 "2024-08-05T21:35:19Z")

</div>

> [@bschubert](#):
>
> random.choice

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

---

<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 5, 2024, 9:38pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/8 "2024-08-05T21:38:15Z")

</div>

Where are the trailing commas?

---

<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 5, 2024, 10:06pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/9 "2024-08-05T22:06:58Z")

</div>

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

---

<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 5, 2024, 10:11pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/10 "2024-08-05T22:11:51Z")

</div>

> [@George2](#):
>
> > [@bschubert](#):
> >
> > random.choice
> 
> Verb is an array of words. I am trying to mathmatically create language

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

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

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

> [@George2](#):
>
> Where are the trailing commas?

For example:

```python
text = "I ",

```

and

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

```

---

<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 5, 2024, 10:24pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/11 "2024-08-05T22:24:02Z")

</div>

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

---

<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 6, 2024, 7:47pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/12 "2024-08-06T19:47:36Z")

</div>

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

---

<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 6, 2024, 11:23pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/13 "2024-08-06T23:23:44Z")

</div>

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.

---

<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 7, 2024, 7:21am UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/14 "2024-08-07T07:21:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [August 7, 2024, 3:03pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/15 "2024-08-07T15:03:25Z")

</div>

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

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

SyntaxError: '[' was never closed

```

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

---

<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 7, 2024, 9:10pm UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/16 "2024-08-07T21:10:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [August 8, 2024, 12:59am UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/17 "2024-08-08T00:59:04Z")

</div>

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.

---

<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 8, 2024, 2:58am UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/18 "2024-08-08T02:58:07Z")

</div>

I’ll remember that. Thanks

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [August 8, 2024, 3:21am UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/19 "2024-08-08T03:21:57Z")

</div>

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

---

<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 8, 2024, 5:00am UTC](https://discuss.python.org/t/syntax-error-i-cant-understand/60051/20 "2024-08-08T05:00:41Z")

</div>

In the upper left it says Python 3

[Next page](https://discuss.python.org/t/syntax-error-i-cant-understand/60051.md?page=2)
