【问题标题】:What is 'local variable referenced before assignment' [duplicate]什么是“分配前引用的局部变量”[重复]
【发布时间】:2015-06-08 13:06:31
【问题描述】:

我正在尝试解决 checkio 的问题(房屋密码) ..我的代码如下

def checkio(data):
    if len(data)>9:
        for i in data:
            if str.isdigit(i)==True:
                global counternumber
                counternumber=counternumber+1
            if str.isupper(i)==True:
                global counterupper
                counterupper=counterupper+1
            if str.islower(i)==True:
                global counterlower
                counterlower=counterlower+1
    if (counternumber>1 & counterupper>1 & counterlower>1):
        return True
else:
    return  False

此函数在尝试实现时弹出以下错误

NameError:未定义全局名称“counterupper”

在声明为全局变量之前会弹出错误

UnboundLocalError: local variable 'counterupper' referenced before assignment,

这些错误是什么意思以及如何解决它们??

请解释清楚,因为我是编程新手..

【问题讨论】:

标签: python python-3.x error-handling


【解决方案1】:

你可以简写:

def checkio(data):
    return (len(data) > 9 and
        any(ch.isdigit() for ch in data) and
        any(ch.isupper() for ch in data) and
        any(ch.islower() for ch in data))

【讨论】:

    猜你喜欢
    • 2012-08-07
    • 2013-02-28
    • 2016-04-07
    • 2021-12-16
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多