<分区>
我一直在尝试在变量中返回函数中的变量并在其外部使用它:
test = 0
def testing():
test = 1
return test
testing()
print(test)
但是当我运行它时,结果是0。我该如何解决这个问题?
<分区>
我一直在尝试在变量中返回函数中的变量并在其外部使用它:
test = 0
def testing():
test = 1
return test
testing()
print(test)
但是当我运行它时,结果是0。我该如何解决这个问题?
最佳答案
您有点搞乱了范围和/或分配。试试这个:
def testing():
test = 1
return test
test = testing()
print(test)
说明:testing
中的test
与模块中的test
不同。您必须在模块级别分配它才能获得预期的结果。
关于python - 在函数中返回变量 Python 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18039530/