gpt4 book ai didi

python - python err : Local variable referenced before assignment

转载 作者:行者123 更新时间:2023-12-02 10:48:43 25 4
gpt4 key购买 nike

我收到错误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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com