Syntax error when using 'global' keyword in eval

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bauklotz
    New Member
    • May 2010
    • 1

    Syntax error when using 'global' keyword in eval

    So, recently I've been experimenting with a telnet server, and a 5250 terminal emutator and I thought of making a field system, for easier graphical (well, not quite) displaying.

    This basically lets you set the value of a X and Y position on the screen and it automatically updating it, and I needed to make the field_[X & Y coordinates] variables global in order to use them in all the functions. The screen size variables work fine, but since it has many diffirent field_* variables which have diffirent names, I had to use eval, but this throws a syntax error.

    The code and error message is located below:

    Code:
    from os import system
    
    def set_screen_size(x,y):
        global screen_x
        global screen_y
        screen_x = str(x)
        screen_y = str(y)
        system('mode con cols=' + screen_x + ' lines=' + screen_y)
    
    def set_field(x,y,val):
        global screen_x
        global screen_y
        x = str(x)
        y = str(y)
        val = val[0:1]
        if(x > screen_x or x < 0 or y > screen_y or x < 0):
            on_error('Field outside screen given. set_field('+x+','+y+','+val+')')
        eval('global field_' + x + y)
        eval('field_' + x + y + ' = ' + val)
        update_screen()
    
    def update_screen():
        global screen_x
        global screen_y
        for x in screen_x:
            line = ""
            for y in screen_y:
                eval('global field_' + x + y)
                eval('val = field_' + x + y)
                line = line + val
            print line
    Traceback:
    Code:
    Traceback (most recent call last):
      File "G:\FieldScreen\fieldscreen.py", line 34, in <module>
        set_field(0,0,"H")
      File "G:\FieldScreen\fieldscreen.py", line 18, in set_field
        eval('global field_' + x + y)
      File "<string>", line 1
        global field_00
             ^
    SyntaxError: invalid syntax
  • woooee
    New Member
    • Mar 2008
    • 43

    #2
    Start with this
    Code:
    if(x > screen_x or x < 0 or y > screen_y or x < 0):
    if(x > screen_x or x < 0 or y > screen_y or y < 0): <=====

    Comment

    Working...