【发布时间】:2016-04-22 16:07:06
【问题描述】:
我是 Python 新手,在尝试将变量传递给后续函数(在最基本的层面上)时,我感到很沮丧。我已经研究过这个网站和其他网站,但没有得到一个合适的明确答案。我不想使用全局变量。我已经根据 IDLE 的错误报告对函数尝试了各种位置参数,但我只是不明白为什么在下面的代码中,前两个函数按预期工作,但第三个函数遵循完全相同的结构,失败。这是代码:
#passing values between functs.
def func1():
initial_value = 'abc'
print(initial_value, "is the initial value.")
value_to_pass = initial_value
return value_to_pass
def func2(value_to_pass):
print("\nIn FUNC2 now. Did the value pass?")
print(value_to_pass, "is the passed value.")
new_value1 = value_to_pass + 'd'
print(new_value1, "is the new value.")
return new_value1
def func3(new_value1):
print("\nIn FUNC3 now. Did the value pass?")
print(new_value1, "is the passed value.")
new_value2 = new_value1 + 'e'
print(new_value2, "is the new value.")
#main
func2(func1())
func3(func2())
input("\nPress the ENTER key to EXIT.")
我尝试将value_to_pass 作为参数添加到#main 下func2 的func3 调用中,正如IDLE 错误所暗示的那样,以及尝试使其工作的其他事物组合,但是没运气。有人可以向我解释为什么以及这里出了什么问题吗?
【问题讨论】:
-
这个问题不属于 SO。即使其他一些新手有类似的问题,他们也永远无法找到这个讨论。尝试例如寻找当地的 Python 聚会来帮助您学习。
标签: python function return local-variables