2017-02-16 109 views
-2

每个人。Python。 NameError:全局名称未定义(功能无类)

我的Python脚本有问题。这里有一个问题代码(有“打印”行只是为了检查变量的值):

def checkForHittingLevel(name, curValue, checkLine): 
match = bool(False) 
if checkLine is None: 
    return match 
for parametersForCheck in checkLine.split(';'): 
    if name in parametersForCheck: 
    actionWithLevel = parametersForCheck.replace(name,'') 
    # Just to check that it's not empty or there is any problem: 
    print actionWithLevel[0] 
    print type(actionWithLevel) 
    if actionWithLevel[0] == '>': 
    match = True if curValue > actionWithLevel[1:] else False 
    break 
    elif actionWithLevel[0] == '<': 
    match = True if curValue < actionWithLevel[1:] else False 
    break 
    elif actionWithLevel[0] == '=': 
    match = True if curValue == actionWithLevel[1:] else False 
    break 
    elif actionWithLevel[0] == '!': 
    match = True if curValue != actionWithLevel[1:] else False 
    break 
    else: 
    match = False 
    break 
return match 

incArgs.add_argument('-c', '--critical-list', type=str, dest='criticals', 
help='List of critical values for checking data') 
inpValue = incArgs.parse_args() 
[... some code here ...] 
for checkLine in dNetCoreStatsOutData.splitlines(): 
checkStatName = str(checkLine.split()[0]) 
    checkStatValue = int(checkLine.split()[1]) 
    for checkPrevDataLine in oldData.splitlines(): 
    if checkStatName in checkPrevDataLine: 
    prevValue = int(checkPrevDataLine.split()[1]) 
    diffValue = checkStatValue - prevValue 
    if checkForHititngLevel(checkStatName, diffValue, inpValue.criticals): 
    ... code here ... 

如果我试图运行该脚本,我得到这样的输出:

> 
<type 'str'> 
Traceback (most recent call last): 
    File "test.py", line ###, in <module> 
    if checkForHitingLevel(some_name, 20, 'some_name>10'): 
    File "test.py", line ###, in checkForHittingLevel 
    if actionWithlevel[0] == '>': 
NameError: global name 'actionWithlevel' is not defined 

如果使用“print”命令,那么处理变量就没有问题。但是当我试图从字符串中只获取特定的字符时,我得到错误。

我不明白为什么会发生。如果这是Python的正常行为,那么如何从行中获取字符(例如,通过附加变量)?我知道的唯一方法是使用“[]”。

PS没有区别,如果我会尝试:

CheckResault = checkForHittingLevel(some_name, 20, 'some_name>10;name_2<10') 

UPDATE:编辑的代码,因为有一些变量名的问题。 Screenshot

UPDATE2:在我的第一个例子中,我只用了函数的一部分以及它应该如何调用。我自己检查了这个例子,它的工作原理。但是在完整的代码中却没有。所以我在调用这个函数的那部分代码中加入了上面的信息。

+0

你认为'actionWithLevel'的定义在哪里?它没有在你当前的代码中定义。 'actionAndlevel','actionWithlevel'和'actionWithLevel'是不同的变量名称,如果这让你感到困惑。 – miradulo

+0

只是为了澄清:'actionWithLevel'和'actionWithlevel'(注意'l')也是不同的名字。 –

+0

只是一个提示:'match = True如果curValue> actionAndLevel [1:]否则False'可以写成match = curValue> actionAndLevel [1:]'。请尊重[Python代码样式指南](https://www.python.org/dev/peps/pep-0008/)并使用4个空格进行缩进。 – Matthias

回答

0

你在开始时定义actionWithLevel。 然后你继续比较actionWithlevel ..你从来没有定义过。这是一个不同的变量还是只是一个错字?您使用较低的“l”而不是大写的“L”。如果你确实想用actionWithlevel(用较低的“l”)进行比较,你必须首先定义它。

0

好的,我确实发现了问题的根源。在定义函数后定义了传递给函数的变量之一。这个变量是由argparse创建的(变量的名字是inpValue.criticals)。 当我在def checkForHititngLevel之前移动​​时,脚本停止显示错误。 我不知道为什么它有这种行为,因为函数中的print命令可以在没有任何问题的情况下使用此变量。

相关问题