def function_tuple(a,b):
sum2 = a + b
product = a*b
return sum,product
print(function_tuple(2,3))
print("Sum is {}".format(sum2))
答案 0 :(得分:0)
NameError
表示您尝试在使用Python的地方使用未知的东西。见f.e.此处:Python NameError: name is not defined
在您的情况下,您尝试访问function_tuple
的本地变量在其外部-该变量不存在。
您还有更多问题:
您返回结果(错误地使用sum
函数“指针”)。
您可以通过将return sum,product
修改为return sum2, product
来解决此问题。最好根本不创建本地人:
def function_tuple(a,b):
# no need to declare local vars, simply return result
# this intrinsically fixes naming problem sum->sum2, sum == sum() function
return a+b, a*b
s,p = function_tuple(2,3) # call function, decompose result into s and p
print(f"Sum is {s} - product is {p}") # use string literal interpolation for python 3
结果:
Sum is 5 - product is 6