python中怎么在自定义函数调用另外一个函数中的参数

def a ():
q=1+2
return q
def b ():
a()
c=2+q
print c
b()
总是出现这个错误,NameError: global name 'q' is not defined
怎么解决???????

>>> def a():
global q
q=1+2
return q
>>> def b():
a()
c=2+q
print(c)

>>> b()
5
>>> q
3

函数中的变量在外面调用,需要申明为全局变量

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-08-12
12345678910111213>>> def a(): global q q=1+2 return q>>> def b(): a() c=2+q print(c) >>> b()5>>> q3
函数中的变量在外面调用,需要申明为全局变量
相似回答