相关疑难解决方法(0)

Python范围

我想弄清楚这个:

c = 1
def f(n):
    print c + n 
def g(n):
    c = c + n

f(1) # => 2
g(1) # => UnboundLocalError: local variable 'c' referenced before assignment
Run Code Online (Sandbox Code Playgroud)

谢谢!

python scope

41
推荐指数
3
解决办法
2万
查看次数

Python - 函数输出?

我有一个非常基本的问题.

假设我调用一个函数,例如,

def foo():
    x = 'hello world'
Run Code Online (Sandbox Code Playgroud)

如何让函数以这样的方式返回x,我可以将它用作另一个函数的输入或者在程序体内使用变量?

当我使用return并在另一个函数中调用该变量时,我得到一个NameError.

python function

10
推荐指数
2
解决办法
7万
查看次数

标签 统计

python ×2

function ×1

scope ×1