2017-11-11 6 views
-1
import random 
import time 
import re 
import sys 
import string 
import os 
from random import * 

def menu(): 
    print("Welcome To A Password Generator And Checker") 
    while True: 
     try: 
      choice = int(input(""" 
1) Generate Password 
2) Check Password 
3) Quit 
""")) 

      if choice == 1: 
       gp() 
      elif choice == 2: 
       print(""" 
- Requirments - 

You Must Include: 
~ Uppercase Letter 
~ Lowercase Letter 
~ Number 
~ Symbol 
""") 
       time.sleep(1) 
       print(""" 
Point System: 
~1 Uppercase Letter = 5 Points 
~1 Lowercase Letter = 5 Points 
~1 From 0-9 = 5 Points 
~1 Allocated Symbol = 5 Points 
~If Has All Add 10 Points 
    """) 
       passwd = input("Enter Your Desired Password: ") 
       passwdVal(passwd) 
      elif choice == 3: 
       os.system("cls") 
       print("Goodbye") 
       time.sleep(0.5) 
       sys.exit() 
     except ValueError: 
        os.system("cls") 
        print("Please select a legitimate option") 







def passwdVal(passwd): 
    points = 0 
    while passwd: 
     if (len(passwd)<8 or len(passwd)>24): 
      print("Your Length Is Either Too Big Or Too Small") 
      print("Try Again") 
      return 
     if not re.search("[a-z]",passwd): 
      break 
     elif not re.search("[0-9]",passwd): 
      break 
     elif not re.search("[A-Z]",passwd): 
      break 
     elif not re.search("[!$%^&()_]",passwd): 
      break 
     else: 
      print("Valid Password") 
      return 



    while points < 35: 
     if passwdVal(passwd): 
      if 8 <= len(passwd) < 24 : 
       print(len(passwd),"Points Added - Length") 
       points += (len(passwd)) 

      else: 
       print("Wrong length, it must be 8 to 24 characters") 
       continue 

      if re.search("[a-z]", passwd): 
       print("5 Points Added - Lowercase Letter") 
       points += 5 

      if re.search("[0-9]", passwd): 
       print("5 Points Added - Number") 
       points += 5 

      if re.search("[A-Z]", passwd): 
       print("5 Points Added - Uppercase Letter") 
       points += 5 

      if re.search("[!$%^&()_]", passwd): 
       print("5 Points Added - Symbols") 
       points += 5 

      if points == 20: 
       points += 10 

      print("You have {} points".format(points)) 
      break 
     else: 
      print("Not a Valid Password") 











def gp(): 
    print(""" 
Generating Password:""") 
    generation = string.ascii_letters+("[!$%^&*()-_=+]")+string.digits 
    gp = ("").join(choice(generation) for x in range(randint(8,12))) 
    time.sleep(1) 





menu() 

問題が他のコードにある場合には、私はコード全体を指定しました。しかし、私が直面している問題は、有効なパスワードを入力すると、有効なパスワードが表示されるということです。しかし、ポイントは表示されません。私は何度も試してみましたが、私は問題を見つけることができません。私が間違ったパスワードを入力すると、私はそれが正しいことを表示します。点の出現に関するPythonの援助

+0

passwd: 'の間にあなたは何を期待しますか? – jbndlr

答えて

0

を印刷した後で、にreturnというステートメントがあります。これは、後続のコード(ポイントを集計したもの)が実行されないことを意味します。

+0

これを修正するために削除しました。有効なパスワードが繰り返しているようです。どのようにそれを修正するための手掛かり。 –

+0

私はそれを何度も修正しようとしています。 –

0

パスワード検証機能を詳しく見ていきましょう。 menu()gp()は、最も良いコードスタイルはありませんが、少なくとも動作するようです。あなたのpasswdVal()

は、限り、あなたのpasswdが空の文字列であるとして、ステップ・バイ・ステップ
points = 0 
while passwd: # Loop while there is a password... 
    if (len(passwd)<8 or len(passwd)>24): 
     print("Your Length Is Either Too Big Or Too Small") 
     print("Try Again") 
     return # Exit function 
    if not re.search("[a-z]",passwd): 
     break # Exit loop 
    elif not re.search("[0-9]",passwd): 
     break # Exit loop 
    elif not re.search("[A-Z]",passwd): 
     break # Exit loop 
    elif not re.search("[!$%^&()_]",passwd): 
     break # Exit loop 
    else: 
     print("Valid Password") 
     return # Exit function 

ですから、Trueに評価される条件とwhile -loopを作成している

をコメントしました。この変数を変更しないので、ループを中断したり、関数を終了する必要がなければ、ループ条件は 常にになります。

パスワードの長さが境界に収まらない場合は、機能全体を終了します。特定のグループに文字がない場合は、ループを終了して関数の後続のコードに進みます。パスワードが有効な場合は、その関数を終了します。

これは、パスワード文字列に表示したくない文字がある場合、インタープリタによってに達するだけで、すべての機能が終了するためです。これは論理的に間違っているようですが、有効なコードです。ここでは不要なので、おそらくwhile passwd:ループを削除する必要があります。 ifブランチのいずれか(つまりエラーが見つかった場合)には、警告を印刷して、汎用のelseブランチでもこの機能を使用したいだけです。機能を途中で終了しないように注意してください。

あなたのその後のコードでオン

:あなたは、パスワードの中に見つけるすべての文字クラスのためのポイントを追加する予定

while points < 35: 
    if passwdVal(passwd): # This is a RECURSIVE CALL! 
     if 8 <= len(passwd) < 24 : 
      print(len(passwd),"Points Added - Length") 
      points += (len(passwd)) 
     else: 
      print("Wrong length, it must be 8 to 24 characters") 
      continue 
     if re.search("[a-z]", passwd): 
      print("5 Points Added - Lowercase Letter") 
      points += 5 
     if re.search("[0-9]", passwd): 
      print("5 Points Added - Number") 
      points += 5 
     if re.search("[A-Z]", passwd): 
      print("5 Points Added - Uppercase Letter") 
      points += 5 
     if re.search("[!$%^&()_]", passwd): 
      print("5 Points Added - Symbols") 
      points += 5 
     if points == 20: 
      points += 10 

     print("You have {} points".format(points)) 
     break 
    else: 
     print("Not a Valid Password") 

。あなたが実際にやっているのは、今までのところ、35ポイント以下になっていれば、すべてのアスペクトのポイントを追加するということです(今は上記の再帰呼び出しを無視しています)。これは、おそらく一度だけではなく複数の側面を評価することを意味します。あなたはここでもループを望んでいません。あなたの側面を歩き、必要に応じてポイントを追加するだけです。最後に、ポイント集計を印刷します。

は、再帰呼び出しをそこで削除する必要があります。これは、関数呼び出しが無限にスタックするためです。

あなたの検証関数を、パスワードの実際の検証を行う関数とパスワードのポイントを計算する別の関数に分割するように考えるべきかのようです。