Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythonNameError:未定义全局名称(没有类的函数)_Python_Function_Runtime Error - Fatal编程技术网

pythonNameError:未定义全局名称(没有类的函数)

pythonNameError:未定义全局名称(没有类的函数),python,function,runtime-error,Python,Function,Runtime Error,各位 我的Python脚本有问题。这是一个问题代码。有打印行仅用于检查变量值: def checkForHittingLevel(name, curValue, checkLine): match = bool(False) if checkLine is None: return match for parametersForCheck in checkLine.split(';'): if name in parametersForCheck: actionWithLev

各位

我的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')
更新:已编辑代码,因为某些变量名存在问题。


UPDATE2:在我的第一个示例中,我只使用了part with函数以及如何调用它。我检查了这个例子本身,它的工作。但在完整的代码中它并没有。因此,我在上面的信息中添加了调用此函数的代码部分。

您在actionWithLevel的开头定义。
然后你继续比较行动和水平。。你从未定义过。这是一个不同的变量还是仅仅是一个输入错误?你用小写l代替大写l。如果您确实想将actionWithlevel与较低的l进行比较,您必须首先定义它

好的,我确实找到了问题的根源。传递给函数的变量之一是在定义函数后定义的。此变量已由argparse创建。变量名称为inpValue.criticals。 当我在def checkforhittinglevel之前使用argparse移动行时,脚本停止显示错误。
我不知道它为什么会有这种行为,因为函数中的print命令可以毫无问题地处理这个变量。

您认为actionWithLevel是在哪里定义的?它未在当前代码中定义。actionAndlevel、actionWithlevel和actionWithlevel是不同的变量名称,如果这让您感到困惑的话。请澄清:actionWithlevel和actionWithlevel注意,l也是不同的名称。提示:如果curValue>actionAndlevel[1:],则match=True;否则False可以写成match=curValue>actionAndlevel[1:]。请尊重这个原则,用4个空格做缩进。嗨,米奇,基里尔。是的,你对下面几行中的名字说得对。这是因为变量的名字是actionWithlevel。所以我认为问题的原因可能是变量的名称,我改变了它。但正如你所看到的,我并不是一开始就这么做的。好啊这次我做对了,更新了主要问题,问题仍然在这里,它也会在这里,因为错误发生在变量名称错误的行之前,您仍然没有修复它。actionAndLevel已定义,actionWithLevel未定义。