姓氏验证码效率 - Python

时间:2017-02-21 20:20:01

标签: python python-3.x variables while-loop coding-efficiency

我在下面写了while语句来验证姓氏字段并输出一个布尔值以查看是否发生了任何错误。该程序将所有错误消息存储在变量errors中。我在每次错误检查后都添加了break语句,因为我不希望程序在检测到错误后继续检查错误。

我不确定此代码是否有效 - 在这种情况下是否需要while语句?

您怎么看?

valcheck = True

# validate surname
while valcheck == True :

    try :
        surname = str(e2.get())
    except :
        errors += "\nSurname not valid - must be a string."      
        valcheck = False
        break

    # check if surname is not empty
    if len(surname) <= 0 :
        errors += "\nSurname cannot be blank."      
        valcheck = False

    # check if surname is alphabetical
    for i in str(surname) :
        # also, allow for hyphens and apostrophes
        if not(i.isalpha() or i == "'" or i == '-') :
            errors += "\nSurname not valid - must be alphabetical."
            valcheck = False

    # if there are no errors, exit the statement
    break

提前致谢。

1 个答案:

答案 0 :(得分:0)

这似乎更好吗?

    else if(keyPressed == Qt::Key_Space){
    emit sigHalted();
}