在函数外部定义的变量不能在函数中使用?

时间:2020-03-01 13:04:36

标签: python function variables

我只是创建函数(Python),并想使用在函数外部定义的变量,并且发生了错误

def outside_variable_show(status):
    if status == 1:
        crrnt_nmbr = crrnt_nmbr + 1
        print (crrnt_nmbr)

status = 1
crrnt_nmbr = 0
print (crrnt_nmbr)
outside_variable_show(1)

注意:“ crrnt_nmbr”必须在函数的内部和外部使用。

请告诉我实现方法。非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试一下:

def outside_variable_show(status):
    global crrnt_nmbr
    if status == 1:
        crrnt_nmbr = crrnt_nmbr + 1
        print (crrnt_nmbr)

status = 1
crrnt_nmbr = 0
print (crrnt_nmbr)
outside_variable_show(1)