【问题标题】:Variable that define outside a function can't use in a function? [duplicate]在函数外部定义的变量不能在函数中使用? [复制]
【发布时间】:2020-03-01 13:04:36
【问题描述】:
我刚刚创建函数(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”必须在函数内外使用。
请告诉我实现它的方法。非常感谢。
【问题讨论】:
标签:
python
function
variables
【解决方案1】:
试试这个:
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)