2017-04-05 12 views
-2

私は、文字列が有効なパスワードであるかどうかをチェックするパスワードチェッカーの作業中です。私は少なくとも8文字があるかどうかを確認しなければならず、文字と数字だけで構成されなければならず、最後の2文字は数字でなければなりません。str.isdigit()がPythonで動作していないようです

これまでのところ、password.isdigit()以外はすべて動作しているようです。パスワードが有効になることがありますが、時にはそうではありません。助言がありますか?

# Gets the users password 
password = input('Enter a string for password: ') 
# Splices the last two characters of the password 
lastTwo = password[-2:] 

# Checks the password if it is less than 8 characters 
while len(password) < 8: 
    print('The password you entered is too short.') 
    print() 
    password = input('Enter a string for password: ') 

    # Checks the password if it is composed of letters and numbers 
    while password.isalnum() == False: 
     print('Your password has special characters not allowed.') 
     print() 
     password = input('Enter a string for password: ') 

    # Checks the spice to verify they are digits 
    while lastTwo.isdigit() == False: 
     print('Your last two characters of your password must be digits.') 
     print() 
     password = input('Enter a string for password: ') 

print('Your password is valid.') 
+1

ないいくつかの入力がどのようなものですが期待どおりに行動する? –

+0

"何か提案" - 実際の問題を探します。 *あなたの*コードおよび/またはあなたの理解の中で。 –

+1

最後の2文字の最後のループの直前にパスワードをスプライスする必要があります。さもなければ、それは前のパスワードの最後の2文字を含むでしょう – kuro

答えて

2

ご提供されたコードに問題のほんの一握りがあります。特に、後続のルールwhile len(password) < 8のみをチェックします。パスワードの長さを10にすると、ルールは決してチェックされません。また、あなたは次のようにそれぞれの新しいパスワードを使用してlastTwoが、これは全体的なwhile声明に包まれif...elif..elif...else...であなたのいくつかのwhile文を置き換えることであろう修正する

一つの方法を試みた更新されません。

# Gets the users password 
password = input('Enter a string for password: ') 

while True: 
    # Checks the password if it is less than 8 characters 
    if len(password) < 8: 
     print('The password you entered is too short.') 
    # Checks the password if it is composed of letters and numbers 
    elif not password.isalnum(): 
     print('Your password has special characters not allowed.') 
    # Checks the spice to verify they are digits 
    elif not password[:-2].isdigit(): 
     print('Your last two characters of your password must be digits.') 
    else: 
     # we only get here when all rules are True 
     break 

    print() 
    password = input('Enter a string for password: ') 

print('Your password is valid.') 

これはあなたが意図したとおりに動作するはずです。しかし、我々はそれをしている間、なぜすべてのパスワードが壊れていると教えてください? UIの観点からは、ユーザーに情報を提供するのに役立ちます。

我々は、関連するルールが満たされているかどうかと一緒に情報メッセージを格納する場合は、我々はすぐにそうように破られたルールのすべて、うまくすることができます

valid_password = False 

while not valid_password: 
    # Get a password 
    password = input('\nEnter a string for password: ') 
    # applies all checks 
    checks = { 
     '- end in two digits': password[-2].isdigit(), 
     '- not contain any special characters': password.isalnum(), 
     '- be over 8 characters long': len(password) > 8 
    } 
    # if all values in the dictionary are true, the password is valid. 
    if all(checks.values()): 
     valid_password = True 
    # otherwise, return the rules violated 
    else: 
     print('This password is not valid. Passwords must:\n{}'.format(
      '\n'.join([k for k, v in checks.items() if not v]))) 

print('Your password is valid.') 
+0

私はあなたの感情に同意しますが。私は実際に(むしろ貧しい)質問に答えるわけではありませんか? – SiHa

+0

@SiHaあなたはもっと直接的な修正を追加するために絶対に右に編集しました – asongtoruin

+0

ありがとう。それはもっと理にかなっていて、もっと凝縮しています。私はまだpythonとその問題のためのコーディングに新しいので、私は助けに感謝しています。 – Drock33

0

whileループ内で値lastTwoを更新することはありません。したがって、ユーザーが最初にパスワードabc123を入力した場合を想像してください。その後、lastTwo23と計算されます。

これで、パスワードが短すぎるとわかり、ユーザーに新しいパスワードを要求します。彼がabcdefghと入力したとします。これで、1回目と2回目のチェックが完了します。ただし、lastTwoはまだ23であるため、3回目のチェックが間違って行われます。

新しいパスワードを受け入れるか、直接、次のようにチェックしたときにあなたは、このようにlastTwoの値を再計算する必要があります

while (password[-2:]).isdigit() == False:

+0

そう、私はちょっと残念ですが、私はそれを認識しませんでした。ありがとうございます – Drock33

関連する問題