UnboundLocalError(分配前引用的局部变量)

时间:2019-04-11 14:05:56

标签: python debugging pycharm

我正在尝试在脚本搜索跟踪编号并打印.txt文档内容的地方做这部分。使用此部分时出现此错误:

def lookup_trackingnum():
    logger.info('lookup_trackingnum started - B3                    ')
    print(' ')
    print(' ')
    print(' = = Look Up a Tracking Number = =  ')
    print(' ')
    print(' ')
    lookup_in = input('Tracking Number: ')
    try:
        lookup_path = 'lms19-files/exports/' + lookup_in + '.txt'
        logger.debug('lookup_path created - B3   -   BUG RESOLVING      ')
        with open(lookup_path, 'r') as lookup:
            logger.debug('lookup_path opened - B3   -   BUG RESOLVING       ')
            print(lookup.read())
            logger.debug('lookup_path printed - B3   -   BUG RESOLVING      ')
    except:
        print('ERROR: The Tracking Number ' + lookup_in + ' doesn\'t exist.')
        logger.error('Tracking Number doesn\'t exist - B2                ')
        print(' ')
        print(' ')
        print(' [E] Return to Dashboard  ')
        print(' ')
        logger.debug('Before lookupnav_in input - B3   -   BUG RESOLVING')
        lookupnav_in = input('Input: ')
        logger.debug('After lookupnav_in input - B3   -   BUG RESOLVING ')
    if lookupnav_in == 'E':
        logger.debug('If lookupnav_in - B3   -   BUG RESOLVING          ')
        startmenu()
        logger.debug('startmenu triggered - B3   -   BUG RESOLVING      ')
    else:
        print(lookupnav_in + ' isn\'t a valid input.')
        logger.error('Invalid Input - B3                                ')
        startmenu()

错误:

Traceback (most recent call last):
  File "/Users/User/PycharmProjects/lms19/lms19-beta.py", line 417, in <module>
    startmenu()
  File "/Users/User/PycharmProjects/lms19/lms19-beta.py", line 280, in startmenu
    lookup_trackingnum()
  File "/Users/User/PycharmProjects/lms19/lms19-beta.py", line 253, in lookup_trackingnum
    if lookupnav_in == 'E':
UnboundLocalError: local variable 'lookupnav_in' referenced before assignment

和日志数据:

lms19 : INFO - Logger Started - B6                                 [2019/04/11 15:20:11]
lms19 : CRITICAL - Setup skipped - B6                              [2019/04/11 15:20:11]
lms19 : INFO - Software Initialised - B6                           [2019/04/11 15:20:11]
lms19 : INFO - Main Script started - B7                            [2019/04/11 15:20:11]
lms19 : INFO - startmenu started - B4                              [2019/04/11 15:20:11]
lms19 : INFO - lookup_trackingnum started - B3                     [2019/04/11 15:20:13]
lms19 : DEBUG - lookup_path created - B3   -   BUG RESOLVING       [2019/04/11 15:20:15]
lms19 : DEBUG - lookup_path opened - B3   -   BUG RESOLVING        [2019/04/11 15:20:15]
lms19 : DEBUG - lookup_path printed - B3   -   BUG RESOLVING       [2019/04/11 15:20:15]

我尝试了在网上(以及在StackExchange上)找到的多项内容,但无法正常工作。

预先感谢, 保罗

2 个答案:

答案 0 :(得分:0)

lookupnav_in = input('Input: ')

在这里定义变量。它位于try语句的except块中。

if lookupnav_in == 'E':

这是您首次使用变量的地方。这只是在except块之外。

除非实际发生异常,否则不执行except块。

因此,请考虑一下,如果try从不抛出异常会发生什么。

该变量从未定义。因此,错误。

答案 1 :(得分:0)

我认为这是因为在您的Except块中声明了变量 lookupnav_in ,然后在Except-block 之外使用if语句进行了检查。如果未触发您的Except-block,则永远不会声明该变量。这意味着您必须在特定的Except块之外的其他地方声明它,然后才能使用if语句对其进行检查。