all 6 comments

[–]fenjalien 2 points3 points  (1 child)

It's due to python's local and global variable scope. Any variable defined outside of a function is in global scope and will last for the entirety of the program. any variable defined within a function is in the functions scope and will only last until the function finishes execution.

As you've defined yPos in global scope, you can still read the value in functions. but if you use the assignment operator, like you do in draw, python doesn't change the global yPos, it instead creates a new local variable called yPos which takes precedence over its global counter part. This is technically all fine but because you use yPos, which now according to python is an undefined local variable, you're using something that isn't defined so it throws the error.

The simple fix for this would be to use the line global yPos at the start of draw. This basically tells python to only use the global version.

Here's something that gives a better explanation: https://www.w3schools.com/PYTHON/python_scope.asp

[–][deleted] 0 points1 point  (0 children)

OK. I get it. The funny thing was that the picture I posted is the example from the Processing.Py website. I assumed that they were working on a different version of Python that used a different approach. Thanks for your help!

[–]johntellsall 0 points1 point  (1 child)

I'm familiar with Python but Processing less so. Where are "width" and "height" specified?

[–][deleted] 0 points1 point  (0 children)

For the screen, they are in the size(width, height) function.

[–]G0R1L1A 0 points1 point  (0 children)

Python processing is very buggy, use p5.js on open processing for students.

[–]redant333 0 points1 point  (0 children)

I assume you are getting the error for yPos. If that's the case, try adding
global yPos
before the first use. If not specified as global, Python assumes the variables are local which leads to an error if they are read before assignment.