python - python err : Local variable referenced before assignment

标签 python error-handling compiler-errors

我收到错误UnboundLocal: Local variable T referenced before assignment,但是不是这样的:

import ...
T = 0

def do_something():
    do_something_else(T) # err at this line
T += 1

def do_something_else(t):
    print t

do_something()

这就是我的代码的样子,因此在分配之前没有引用。 (如果我做错了,请纠正我)怎么了?

最佳答案

T声明为全局变量:

def do_something():
    global T # <--------------
    do_something_else(T) # err at this line
    T += 1

关于python - python err : Local variable referenced before assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18286226/

相关文章:

json - 戈朗 : Type [type] is not an expression; json config parsing

Python np.asarray 不返回真实形状

python - Discord.py(重写): Error Handling “Improper Token” In Function

bash - 我试图使用MSYS运行./configure文件,但是在意外 token 附近收到语法错误?

c# - WebSockets 我需要什么 'using' 指令

swift - 如何: Detect if a observable has not emitted any events for specific time in RxSwift

python,列出两个标签之间的所有内容

python - 将 spaCy 模型与 AllenNLP Interpret 或 Textattack 结合使用

python - 学习 : Evaluate performance of each classifier of OneVsRestClassifier inside GridSearchCV

python - 一种包装另一个脚本以在python中进行错误处理并保持相同的命令行API的优雅方法