小编Vik*_*tor的帖子

Python,传递给另一个函数的多个变量的简单示例

我知道之前已经解释过这个问题了,但是我仍然无法弄清楚我的场景,我解释的很简单:

def func1 ():
  a = 1
  b = 2
  print a + b

def func2 ():
  c = 3
  d = 4
  e = a * c
  f = b + d

func1()
func2()
Run Code Online (Sandbox Code Playgroud)

当像这样运行时:

$ ./test1.py 
3
Traceback (most recent call last):
  File "./test1.py", line 18, in <module>
    func2()
  File "./test1.py", line 14, in func2
    e = a * c
NameError: global name 'a' is not defined
Run Code Online (Sandbox Code Playgroud)

简单的问题是,如何更改上面的代码,以便func2存储来自func1的变量?

python variables function

-1
推荐指数
1
解决办法
58
查看次数

标签 统计

function ×1

python ×1

variables ×1