Python。名称错误:未定义全局名称(没有类的函数)

2024-06-26 00:23:52 发布

您现在位置:Python中文网/ 问答频道 /正文

所有人。在

我的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 ...

如果我试图运行脚本,我会得到以下输出:

^{pr2}$

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

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

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

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

更新:编辑代码,因为某些变量名有问题。 Screenshot

更新2:在我的第一个例子中,我只使用了part with function以及如何调用它。我自己检查了这个例子,它起作用了。但在完整的代码中却没有,所以我在调用这个函数的代码部分上面添加了信息。在


Tags: 代码nameinfalsetrueforifmatch
1条回答
网友
1楼 · 发布于 2024-06-26 00:23:52

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

相关问题 更多 >