Python: NameError

Asked by Rocco

So I have the following script section:

def TryCreation():
    try:
        CreateLoop();
    except Exception:
        return false;
    popup("True: " + acc);
    return true;

def MainLoop():
    while True:
        if TryCreation():
            acc+=1;
        if !TryCreation():
            #closeWindows()
            acc-=2;

popup("Initialized...")
MainLoop()

-------------------------------

And upon executing, I receive the following error:

[error] script [ RSAccMake ] stopped with error in line 61
[error] NameError ( global name 'false' is not defined )
[error] --- Traceback --- error source first line: module ( function ) statement 48: main ( TryCreation ) return false;
54: main ( MainLoop ) if TryCreation():
[error] --- Traceback --- end --------------

Not sure how to fix it (excuse my ignorance to the topic). Thank you!

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Python knowledge:
not: false
but: False

Revision history for this message
RaiMan (raimund-hocke) said :
#2

and the next NameError will be with
return true

must be
return True

... and then you will get a problem with acc, since it is defined inside def MainLoop() and hence local.
in TryCreation acc will always show up as None

def TryCreation(acc):
    try:
        CreateLoop();
    except Exception:
        return false;
    popup("True: " + acc);
    return true;

def MainLoop():
    acc = 0
    while True:
        if TryCreation(acc):
            acc+=1;
        if !TryCreation(acc):
            #closeWindows()
            acc-=2;

popup("Initialized...")
MainLoop()

the other option would be to use global:
def TryCreation():
    global acc
    try:
        CreateLoop();
    except Exception:
        return false;
    popup("True: " + acc);
    return true;

def MainLoop():
    global acc
    while True:
        if TryCreation():
            acc+=1;
        if !TryCreation():
            #closeWindows()
            acc-=2;

popup("Initialized...")
acc = 0 # makes it a global variable
MainLoop()

Can you help with this problem?

Provide an answer of your own, or ask Rocco for more information if necessary.

To post a message you must log in.