Processing.py-Bugs icon indicating copy to clipboard operation
Processing.py-Bugs copied to clipboard

I'm getting a NameError I don't think was supposed to happen

Open Hinomupi opened this issue 6 years ago • 6 comments

I'm new to programing. I wrote a program and I'm getting the error "NameError: global name 'taman' is not defined" even though I have already defined taman. Here's the code:

def setup():
    global taman
    global linhas
    global colunas
    global start
    try:
        file = open("settings.txt","r")
    except FileNotFoundError:
        file = open("settings.txt","w")
        file.write("Número de colunas:\n3\nNúmero de linhas:\n3\nRaio:\n30\nEspaço (~distância entre as figuras):\n10\nVelocidade (em rad/s):\n0.01\nFase do x (fração do pi):\n0\nFase do y (fração do pi):\n0\nEspessura do traçado:\n3")
        file.close()
        file = open("settings.txt","r")
    taman = file.read().split('\n')
    file.close()
    file.close()
    for x in [1,3,5,7,15]:
        taman[x] = int(taman[x])
        print(taman[x])
    taman[9] = float(taman[9])
    for x in [11,13]:
        print(taman[x])
        taman[x] = PI*float(taman[x])
    start = True
    size((taman[7]+2*taman[5])*(taman[1]+1),(taman[7]+2*taman[5])*(taman[3]+1))
    colunas = [bolinha((x + (1/2))*(taman[7] + 2*taman[5]) + taman[5],taman[5]+(taman[7])/2,x*taman[9],taman[5],taman[11]) for x in range(1,taman[1]+1)]
    linhas = [bolinha(taman[5]+(taman[7])/2,taman[5]+(x + (1/2))*(taman[7] + 2*taman[5]),x*taman[9],taman[5],taman[13]) for x in range(1,taman[3]+1)]
    background(0)

While it does print the elements of taman when I convert the strings to integers or floats, it raises the error in this line:

size((taman[7]+2*taman[5])*(taman[1]+1),(taman[7]+2*taman[5])*(taman[3]+1))

Did I do something wrong or is this a bug?

Hinomupi avatar Dec 28 '18 21:12 Hinomupi

Hi @Hinomupi ! I noticed your comments in Portuguese. You can send me an email abav (at) lugaralgum (dot) com. To make the code formatting work here you can use tripple-backticks (```) on a line before and after the code.

villares avatar Dec 28 '18 21:12 villares

This question probably belongs in the Processing forums, and not here.

The code sample you've provided is not complete, and would not cause the error you describe.

jdf avatar Dec 28 '18 21:12 jdf

  • I think maybe there is an issue with FileNotFoundError (I looked at the docs) but I could be wrong.

Things you could also watch out:

  • FileIO examples on the IDE
  • If you use non-ASCII chars on the text you might need to prefix the string like u"Número de colunas:\n3\n"

villares avatar Dec 28 '18 21:12 villares

I just edited the post to include where the error occours (what line Processing highlights)

Hinomupi avatar Dec 28 '18 22:12 Hinomupi

Look, you can't use variables inside size()! Try removing or commenting out size(). And I'm using IOError instead of FileNotFoundError

Now bolinhas() is undefined... you should close this :)

villares avatar Dec 28 '18 22:12 villares

bolinha is a class I've defined before, I've only pasted here what I'm defining to be setup().

I wanted to make a program that sets the size of the window according what's set in settings.txt. If I can't use variables inside size() is there any way for me to do this?

edit: The reference in py.processing says "As of Processing 3.0, to use variables as the parameters to size() function, place the size() function within the settings() function (instead of setup()). There is more information about this on the settings() reference page.", however I couldn't find the reference page for settings in py.processing. So I went to the reference in processing.org and now I've managed to fix my problem. Thanks for the help, villares!

Hinomupi avatar Dec 28 '18 22:12 Hinomupi