I need a little help

Im having trouble with getting this to run in python 3.5.3

import numpy as np
import keyboard
from matplotlib import pyplot as plt
one = 1
mapa = [[1, 1, 1, 1, 1],
        [1, 0, 0, 0, 1],
        [1, 0, 1, 0, 1],
        [1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1]]
posx, posy = (1, 1)
exitx, exity = (3, 3)
rot = np.pi/4
for one in range (60):
    rot_i = rot + np.deg2rad(1 - 30)
    x, y = (posx, posy)
    sin, cos = (0.02* np.cos(rot_i))
    n = 0
    while True:
        x, y = (x + cos, y + sin)
        n = n+1
        if mapa [int(x)][int(y)] !=0:
            h = 1/(0.02 + n)
            break
    plt.vlines(1, -h, h)
plt.show()

this error is shown

File “”, line 13
plt.show()
^
SyntaxError: invalid syntax"

Please format your code by placing them inside triple backticks, like this

```
your code goes here
```

This makes it possible for us to read, therefore to help you.

Also, why are you using python 3.5? That version is out of support a long time ago. Please upgrade to the latest version, being 3.12, or at least 3.11 if for some reason you need an older version.

Im using 3.5 because vscode, what I use for python, will not let me get numpy, keyboard, and matplotlib modules above 3.5.7

I see nothing here that would care about using an older version of Python like that, not one that recent, anyway.

If you pasted all of this code at the interpreter prompt, Python will complain because there is no blank line after the for loop. The interpreter expects one line at a time, and when you use anything that makes a block like for, if etc., it wants a blank line to know when you’re done with that block. Then when it receives the whole block, that unindented line doesn’t fit with it.

To solve that problem, you should use an actual file for your code, and make sure that you tell VSCode to run the entire file - not a line at a time.

This doesn’t make any sense. The version numbers for those modules don’t have anything to do with the Python version number, and VSCode will use the standard, built-in tools (like Pip) to install those packages, which will normally get the most recent versions of those packages that will work for your Python version. keyboard is a little out of date, but I had no problem installing and importing it in a test 3.12 environment. The others are actively maintained and definitely support Python 3.12.

1 Like

Hi,

there appears to be an error in an assignment statement in your script:

On the left-hand side, you have a tuple of two variables, sin and cos. On the right side of the equal sign, you have only one term.

You either need to add another term, after a parenthesis on the right side of the equation, or delete a variable from the left side of the equation.

Btw, what is the purpose of the keyboard module? It appears that it is not being used in this script.

This line:

x is fixed. This means that each successive result will not have a unique x value on the graph. Each successive result will be displayed one over the other. Is this what you want? Don’t you want to have a unique value of x for each value of y?

Take a look at this example for multiple plots on the same graph.

Make changes to your code to satisfy these requirements.

Test code:

import numpy as np
# import keyboard
from matplotlib import pyplot as plt
one = 1
mapa = [[1, 1, 1, 1, 1],
        [1, 0, 0, 0, 1],
        [1, 0, 1, 0, 1],
        [1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1]]

posx, posy = (1, 1)
exitx, exity = (3, 3)

rot = np.pi/4

# I created these two variables for testing purposes only
h = []
index = 0

for one in range (60):

    rot_i = rot + np.deg2rad(1 - 30)
    x, y = (posx, posy)
    sin, cos = (0.02* np.sin(rot_i)), (0.02* np.cos(rot_i))
    n = 0

    while True:
        x, y = (x + cos, y + sin)
        n = n+1

        if mapa [int(x)][int(y)] != 0:
            # using the created test variable
            h.append(1/(0.02 + n ))
            print('The value of h[index] is: ', h[index])

            plt.vlines(index, -100*h[index], 100*h[index])
            index += 1
            break

plt.show()

Is this issue specific to older versions of Python? I didn’t notice this problem in Python 3.11 with any of my loops.

Why does the error occur at line 13 when plt.show() is really at line 25 in the code you’re showing us? Can you show us the actual code that causes the error?

Also – and I appreciate that this can be hard, especially for beginners – try to use a more substantive title than “I need a little help”. Perhaps “syntax error with matplotlib”? Even that may be something of a red herring in this case, but it may bring the appropriate experts to your problem.

No. It’s specific to the REPL.

In any version of Python you can write code on the next line after the loop in a source file without a problem. But it will not work at the REPL. Here’s a demonstration for you in 3.11:

$ cat > lineafterloop.py
for x in ():
    pass
print("this line is ok")
$ python3.11
Python 3.11.2 (main, Apr  5 2023, 03:08:14) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in ():
...     pass
... print("this line is not ok")
  File "<stdin>", line 3
    print("this line is not ok")
    ^^^^^
SyntaxError: invalid syntax
>>> import lineafterloop
this line is ok
>>> for x in ():
...     pass
... 
>>> print("this line is also ok because of the extra newline")
this line is also ok because of the extra newline

It’s the same in every version.

Got it! I’m still learning here. I was referring to code in a .py file, you were referring to the REPL.

That does make sense for the REPL though.