簡體   English   中英

Python不遍歷字符串中的所有字符(返回“ None”?)

[英]Python not iterating through all characters in a string (returns 'None'?)

我正在嘗試創建一個程序,該程序可以遍歷密碼中的字符以確定其是否符合規范。 它至少需要七個字符,一個大寫字母,一個小寫字母和一位數字。 這是我所擁有的:

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

有人可以幫忙嗎?

您的函數沒有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

正如@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])
>>> 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語句來返回內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM