【问题标题】:Python not iterating through all characters in a string (returns 'None'?)Python没有遍历字符串中的所有字符(返回'None'?)
【发布时间】:2016-11-30 05:36:24
【问题描述】:

我正在尝试创建一个程序,它可以遍历密码中的字符以确定它是否符合规范。 它至少需要七个字符、一个大写字符、一个小写字符和一个数字。这是我所拥有的:

def validPass(password): # >= 7 chars, one upper, one lower, one digit
for ch in password:
    if ch.isdigit():
        if ch.isupper():
            if ch.islower():
                if len(password) >= 7:
                    print ' Your password is valid.'
                else:
                    print 'Your password is not the correct length.'
    else:
        if ch.isupper():
            if ch.islower():
                if len(password) >= 7:
                    print ' Your password is valid.'
                    break
                else:
                    print 'Your password is not the correct length.'
            else:
                print 'Your password is not the correct length.'
        else:
            if ch.islower():
                if len(password) >= 7:
                    print ' Your password is valid.'
                    break
                else:
                    print 'Your password is not the correct length.'
            else:
                if len(password) >= 7:
                    print ' Your password is valid.'
                    break
                else:
                    print 'Your password is not the correct length.'
print validPass('$$$$$$$')

我知道我在这里找错树了。另外,当我提交 '$$$$$$$' 时,我得到:

Your password is valid.
None

谁能帮忙?

【问题讨论】:

  • 看来你唯一真正的测试是len(password) >= 7 为什么所有复杂的嵌套if 语句。你没有 returning 任何东西,那么你希望 print validPass('$$$$$$$') 打印什么?

标签: python list loops passwords


【解决方案1】:

你的函数没有 return 语句,你试图打印函数的返回值,然后是 None。

顺便说一句,你的函数定义下的块应该缩进。

查看与示例的区别。

In [7]: def foo():
    return 'Hai'
   ...: 
In [8]: print foo()
Hai
In [9]: def foo():
    print 'Hai'
   ...:     
In [10]: print foo()
Hai
None

【讨论】:

    【解决方案2】:

    正如@Dim 所说,您的函数没有返回任何内容,因此无需在函数上调用print

    嵌套 if 语句方法的问题是您必须重新检查每对 if/else 块中的其他要求,这会增加很多冗余。

    这是另一种方法:

    保留密码要求的跟踪器变量,初始化为False。遍历password 中的字符,如果满足要求,将跟踪器变量设置为True

    例子:

    def validPass(password):
        # tracker variables for the password requirements
        upper, lower, digit, length = False, False, False, False
        if len(password) >= 7:
            length = True
        for ch in password:
            if not lower and ch.islower():
                lower = True
            if not upper and ch.isupper():
                upper = True
            if not digit and ch.isdigit():
                digit = True
        # add print statements based on tracker variables if necessary
        # return True/False indicating password validity
        return all([upper, lower, digit, length])
    

    【讨论】:

    • OP:谢谢,这有帮助!
    【解决方案3】:
    >>> pwd = '1frSpuh`
    

    制作一些变量来存储计数

    >>> n_upper = 0
    >>> n_lower = 0
    >>> n_digit = 0
    >>> 
    

    遍历字符串并测试每个字符,统计你感兴趣的字符

    >>> for c in pwd:
        if c.isdigit():
            n_digit = n_digit + 1
        if c.islower():
            n_lower = n_lower + 1
        if c.isupper():
            n_upper = n_upper + 1
    

    应用标准

    >>> if n_upper >= 1 and n_lower >= 1 and n_digit >= 1 and len(pwd) >=7:
        print('valid')
    else:
        print('NOT!!!')
    

    这样的互斥条件的嵌套条件

    if ch.isdigit():
        if ch.isupper():
            if ch.islower():
    

    永远不会工作 - 一个角色不可能同时是三个。


    函数需要一个 return 语句来返回一些东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 2023-03-31
      • 2015-06-18
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多