Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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
Python UnboundLocalError:赋值前引用的局部变量-除非它不是';T全局/局部范围的问题_Python_Python 3.x_Scope - Fatal编程技术网

Python UnboundLocalError:赋值前引用的局部变量-除非它不是';T全局/局部范围的问题

Python UnboundLocalError:赋值前引用的局部变量-除非它不是';T全局/局部范围的问题,python,python-3.x,scope,Python,Python 3.x,Scope,所以我知道这个问题被问了很多次,但我还没有发现任何与我所经历的事情完全相符的东西 在我的程序(Python3.7.1)中,启动时我想定义安装程序的位置,以及保存程序生成的文件的默认位置。稍后在程序中,我想再次显示这些变量,并为用户提供将默认位置更改为其他位置的选项 因此,我在全球范围内有以下代码: import os installedDirectory = os.getcwd() os.chdir('C:\\Program Files') workingDirectory = os.ge

所以我知道这个问题被问了很多次,但我还没有发现任何与我所经历的事情完全相符的东西

在我的程序(Python3.7.1)中,启动时我想定义安装程序的位置,以及保存程序生成的文件的默认位置。稍后在程序中,我想再次显示这些变量,并为用户提供将默认位置更改为其他位置的选项

因此,我在全球范围内有以下代码:

import os

installedDirectory = os.getcwd()

os.chdir('C:\\Program Files')

workingDirectory = os.getcwd()

print ('Program install location = ' + installedDirectory)
print ('Working directory = ' + workingDirectory)
程序的上述部分工作正常,并给出以下结果:

程序安装位置= C:\Users\Chuu\AppData\Local\Programs\Python\Python37-32\Scripts 工作目录=C:\Program Files

但是,在程序的后面,我在函数中调用了这些相同的变量,并为用户提供了更改变量workingDirectory的选项:

def hiimafunction():

    print ('')
    print ('Current program install location = ' + installedDirectory)
    print ('')
    print ('Current working directory where files are saved = ' + workingDirectory)
    print ('')
    print ('Where would you like files to be saved?')
    print ('')
    print ('Press 1 for the current program install location')
    print (r'Press 2 for the default startup location - C:\\Program Files')
    print ('Press 3 for a custom location')
    print ('')
    oliviahye = input()
    if oliviahye == ('1'):
        os.chdir(installedDirectory)
    elif oliviahye == ('2'):
        os.chdir('C:\\Program Files')
    elif oliviahye == ('3'):
        print ('')
        print ('Type a path where you would like to save files.')
        oliviahye = input()
        os.chdir(oliviahye)
    else:
        hiimafunction()

    workingDirectory = os.getcwd()

    print ('')
    print ('Current save location is now = ' + workingDirectory)
发生这种情况时,我会出现以下错误:

UnboundLocalError:之前引用的局部变量“workingDirectory” 分配

尽管错误明确指向该行:

print ('Current working directory where files are saved = ' + workingDirectory)
…问题似乎在于函数中的这一行:

workingDirectory = os.getcwd()
如果我删除函数中的这一行,就不会有错误,但当然程序也不会执行它应该执行的操作,即获取新分配的工作目录并将其分配给变量workingDirectory。我也可以给它一个新的变量(例如workingDirectory2),它不会崩溃,但这样一来,让变量workingDirectory首先显示这些数据的目的就失败了。我不明白为什么这一行在全局范围内没有问题,但当变量已经定义时,同一行在函数中生成错误

阅读此问题,添加以下行:

global workingDirectory
workingDirectory = os.getcwd()
进入程序的第一部分应该会有帮助,因为它应该停止程序在本地创建另一个同名变量,但它似乎没有做任何事情,因为相同的错误会产生。有人能帮忙吗?

如果您在函数中的任何位置指定了一个名称,那么该名称对于整个函数来说都是本地的

所以,因为你有这条线:

global workingDirectory
workingDirectory = os.getcwd()
在函数的下面,名为
workingDirectory
的全局变量是否存在并不重要;当您尝试执行以下操作时:

print ('Current working directory where files are saved = ' + workingDirectory)
它检查为该名称保留的局部变量中的插槽,发现该插槽为空,并引发错误

如果要以独占方式使用全局名称(因此在分配时,全局名称已更改),请添加:

到函数顶部(在函数内部,但在函数中的所有其他代码之前),并确保在调用此函数之前(例如,在顶层分配
workingDirectory=“Uninitialized”
),或在函数使用之前,在函数内部全局指定了一些值(例如,将工作分配移动到使用它的
打印
之前的
工作目录


如果要
打印
全局值,但随后存储该值而不进行更改,请为本地范围的版本使用不同的名称,并且仅读取全局值(在这种情况下,您不需要显式地声明它
global
;只要您从未分配它,Python就假定它需要在嵌套的、全局的和内置的作用域中按顺序查找它)。