2016-11-30 15 views
0

パスワードに含まれる文字を繰り返して、仕様を満たしているかどうかを判断できるプログラムを作成しようとしています。 少なくとも7文字、大文字1文字、小文字1文字、および1桁が必要です。ここに私が持っているものがあります:Pythonは文字列内のすべての文字を反復しません( '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 

誰でも手伝いできますか?

+0

あなたの唯一の本当のテストは、すべての複雑なネストされた 'if'ステートメントがなぜ' len(password)> = 7'であるかのようです。あなたは 'return'何もしないので、あなたは' validPass( '$$$$$$$')を印刷して印刷しますか? – AChampion

答えて

3

関数にはreturn文がなく、関数の戻り値を出力しようとしています。これはNoneです。

ところで、あなたの定義の下にあるブロックはインデントされるべきです。

例との違いを参照してください。 @Dimが言ったように機能上printを呼び出す必要はありませんので

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

、あなたの関数は何も返していません。

ネストされた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]) 
+0

OP:ありがとう、これが助けになった! – DoNotPutMeInABox

+0

助けがあればupvote/acceptを忘れないでください:) – victor

0
>>> 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(): 

ます決して仕事のような相互に排他的な条件でのネストされた条件 - 文字はすべての3つのことはできません。


何かを返すにはreturn文が必要です。