【问题标题】:UnboundLocalError while running in else case in python在python中以else case运行时出现UnboundLocalError
【发布时间】:2022-06-27 22:49:38
【问题描述】:

我写了一个函数,在定义为“键”的文本文件中查找一些字符串, 如果找到所有键,该函数将返回 True 并打印 ok。

如果未找到特定键,该函数会将选中的键附加到列表中。 如果条件不是 True [其他情况],该函数将返回 False,并打印包含文件中所有缺失键的列表。

目前,True 案例运行良好,else 案例 [如果缺少一个或多个键] 给我以下错误:

UnboundLocalError:在赋值之前引用了局部变量“name_key_flag”[我认为这是由于缩进问题/或超出范围的全局变量]

感谢您的帮助,这是我的代码:


import datetime
import os.path
import logging

recipe_name = 'Sanity_CS.py'
NR_log = 'NR_' + str(datetime.datetime.now()).split()[0] + '.log'
lst = []


def pre_conditions():
    with open(NR_log, 'r') as logfile:
        name_key = recipe_name
        app_key = 'Application was powered-up successfully, mode is: Review'
        api_key = 'API recipe was chosen'
        lot_key = 'Lot was created successfully'
        recipe_key = 'Recipe execution started'
        wafer_key = 'The Wafer was loaded successfully'
        recipe_pause_key = 'Recipe run is paused'
        program_key = 'Moving to Program mode'
        recipe_creation_key = 'Recipe was saved successfully under the name: sanity_2022-06-22_Ver_5.1'
        lst1 = lst
        for num, line in enumerate(logfile, 1):
            if name_key in line:
                name_key_flag = 1
            else:
                lst.append('\nError: Script was not successfully executed \n')

            if app_key in line:
                app_key_flag = 1
            else:
                lst.append('\nError: Application was failed to power up.\n')

            if api_key in line:
                api_key_flag = 1
            else:
                lst.append("\nError: Recipe type [API] was not successfully chosen\n")

            if lot_key in line:
                lot_key_flag = 1
            else:
                lst.append("\nError: A lot was not successfully created.\n")

            if recipe_key in line:
                recipe_key_flag = 1
            else:
                lst.append("\nError: A timeout, recipe was not executed\n")

            if wafer_key in line:
                wafer_key_flag = 1
            else:
                lst.append("\nError: The wafer was not loaded.\n")

            if recipe_pause_key in line:
                recipe_pause_key_flag = 1
            else:
                lst.append("\nError: The recipe was not paused.\n")

            if program_key in line:
                program_key_flag = 1
            else:
                lst.append("\nError: The script was not switch to program key.\n")

            if recipe_creation_key in line:
                recipe_creation_key_flag = 1
            else:
                lst.append("\nError: The recipe was not saved.\n")

        if (
                name_key_flag == 1 and app_key_flag == 1 and api_key_flag == 1 and lot_key_flag == 1 and recipe_key_flag == 1 and wafer_key_flag == 1 and recipe_pause_key_flag == 1 and program_key_flag == 1 and recipe_creation_key_flag == 1):
            return True, print("Pre conditions are OK.")

        return False, print("false")  # falsecase(lst1) - printing list function

【问题讨论】:

  • if not lst:替换大的if
  • 这个错误可以通过在你的for循环前设置name_key_flag = None来解决

标签: python


【解决方案1】:

在 else 情况下,您使用 name_key_flag 却没有给它一个值,从而导致此错误。要修复它,请在 for 循环之前为其指定一个值,例如 name_key_flag = 0

【讨论】:

  • 解决了!!谢谢!!
猜你喜欢
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多