我知道这有几个问题,我读过但我无法理解。我试图在另一个函数中使用一个函数的返回结果:
def multiplication(a):
c = a*a
return c
def subtraction(c):
d = c - 2
return d
print(subtraction(c))
Output:
NameError:名称“c”未定义
我知道可以使用全局变量,但我发现这不是一个好主意,因为变量可以更改其值。
EDIT:
这两个函数只是愚蠢的例子。我有两个带有单词的函数,我需要在第二个函数中使用第一个函数的返回值。在我的愚蠢示例中,我需要第二个函数中第一个函数 (c) 的结果。
您没有正确调用您的函数。
def multiplication(a):
c = a*a
return c
def subtraction(c):
d = c - 2
return d
# first store some value in a variable
a = 2
# then pass this variable to your multiplication function
# and store the return value
c = multiplication(a)
# then pass the returned value to your second function and print it
print(subtraction(c))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)