I like to deconstruct my larger more complicated scripts into smaller files. It helps me stay organized.
I'm trying to get imported functions to use global variables that I have created but, it's not working the way I like and I'm not sure why.
I have a main script that defines the global variables. The main script also imports demo.py and calls the function draw.
The demo.py script is basically an if/else test. I've imported another file named shapes.py which has functions to draw shapes.
-If the global variable 'w' is less than 6 call function myrectangle() from shapes.py
-If the global variable 'w' is greater than 6 call function myoval() from shapes.py
-if the global variable 'w' is equal to 6 print "Hello World" in a Alert Box.
When i try to run i get: NameError: global name 'x' is not defined. I thought that if I had global variables defined in the main script i would be able to use them in the imported functions. Am I doing something wrong?
main.py
import vs
import demo
w=vs.PLineLength
h=vs.PBoxWidth
x = 0
y = 0
y = y-h/2
demo.draw()
demo.py
import vs
import shapes
def draw():
vs.PenSize(2);
if w < 6:
shapes.myrectangle()
elif w > 6:
shapes.myoval()
elif w == 6:
vs.AlrtDialog('Hello world.')
shapes.py
import vs
def myrectangle():
vs.Rect(x,y, x+w,y+h)
def myoval():
vs.Oval(x,y, x+w,y+h)