[英]Calling functions into another function and using its variables
def func1():
x = 100
john = 'hello'
return x, john
def func2():
func1()
y = x
return y
print(func2())
因此,這將返回錯誤:
NameError: name 'x' is not defined
有人可以解釋如何在func2中使用func1的變量,並解釋在func2中調用func1的工作方式。
def func1():
x = 100
john = 'hello'
return x, john
def func2():
x, john = func1()
y = x
return y
print(func2())
x
對於func1是本地的(例如john
)。 但這是函數的返回值之一; 所以用吧!
def func1():
x = 100
john = 'hello'
return x, john
def func2():
x,john=func1()
y = x
return y
print(func2())
如果要從func1返回兩個變量,則func1的結果也應該傳遞給其他變量。 因此,您應該添加:
x,john=func1()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.