use*_*308 1 python variables return-value
我一直在尝试在变量函数中返回一个变量,并在它之外使用它:
test = 0
def testing():
test = 1
return test
testing()
print(test)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,结果是0.我怎么能解决这个问题?
你搞砸了范围和/或任务.试试这个:
def testing():
test = 1
return test
test = testing()
print(test)
Run Code Online (Sandbox Code Playgroud)
说明:内部与模块test内部testing不同test.您必须在模块级别分配它以获得预期结果.