2017-11-22 11 views
-3

これは私のcoourseの仕事です。強力なパスワードを生成するプログラムを作成することを意味しています。私はもともとtkinterでこれをやっていましたが、問題を見つけましたので、最初にテキストベースにすることを決めました。そして、最後にカップルの馬が残っていたら、それをtkinterに変換します。NameError:name 'userpword'が定義されていません

私はなぜ理解していないが、私はこのエラーを取得:

Traceback (most recent call last): 
    File "C:/Users/Admin/Desktop/course work.py", line 148, in <module> 
    menureturn() 
    File "C:/Users/Admin/Desktop/course work.py", line 65, in menureturn 
    checktheuserspassword() 
    File "C:/Users/Admin/Desktop/course work.py", line 26, in checktheuserspassword 
    if re.search("[0-9]", x): 
NameError: name 'x' is not defined 

私はもののnuberではなく、私は問題が何であるかを理解しない正直に言うと、それを解決するために期待するものを試してみました。私は助けをオンラインで検索しかし、私は唯一の私の場合は仕事をするからあまりにも切り抜いたawnsersを見つけ

これは私の全体のコードです(私はほとんど2時間になりました。この問題で立ち往生してきた):。

import random 
import re 
#import tkinter.messagebox as tm 
def menureturn(): 
    print("Select one of the following numbers: ") 
    print("1) Check Password")  
    print("2) Generate Password") 
    x=input(print("3) Quit")) 
    if x=="1": 
     def checktheuserspassword(): 
      def userpasswordlength(): 
       userpword=input("Please enter a password you wish to be checked: ") 
       if len(userpword)<=8 : 
        print ("Your password is too short, please enter a longer one (8-12 characters") 
        userpasswordlength() 
       if len(userpword)>=12 : 
        print ("Your password is too long, please enter a shorter one (8-12 characters") 
        userpasswordlength() 
       x=len(userpword) 
      userpasswordlength() 
      point=0 
      contain=1 
      symbol=set("[QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890!$%^&*()-_=+]") 
      point+=int(x) 
      if re.search("[0-9]", userpword): 
       point += 5 
       contain+= 1 
      if re.search("[a-z]", userpword): 
       point+=5 
       contain+= 1 
      if re.search("[A-Z]", userpword): 
       point+=5 
       contain+= 1 
      if any((c in symbol)for c in userpword): 
       point+=5 
       contain+= 1 
      if contain==5: 
       point+=10 
      for line in userpword.splitlines(): 
       line=line.strip() 
       if re.search(r'^[a-z]+$',line): 
        continue 
       if re.search(r'^[A-Z]+$',line): 
        continue 
       if re.search(r'^[a-zA-Z]+$',line): 
        point -=5 
      if userpword.isdigit(): 
       point -= 5 
      if userpword.islower(): 
       point -= 5 
      if userpword.isupper(): 
       point -= 5 
      if re.match("^[!$%^&*()-_=+]*$", userpword): 
       point -= 5 

      if (point > 20): 
       print("Your passwords strength is strong.") 
      if (point > 10) and point<20: 
       print("Your passwords strength is decent.") 
      if (point < 20): 
       print("Your passwords strength is weak.") 
      else: 
       checktheuserspassword() 
     checktheuserspassword() 
      #checker for users password 
     print("--------------------------------------------------------------------------------") 
     backfromgentomenu=input("Type \"Back\" to go back to the menu \nor \"Quit\" to quit: ").lower() 
     if backfromgentomenu=="back": 
      menureturn() 
     if backfromgentomenu=="quit": 
      quit 






    if x=="2": 
     print("--------------------------------------------------------------------------------") 
     print("Password Generator") 

     #passwordstrength=input("Please type the strength you wish your randomly generated password to be (Strong,Medium or Weak)").lower() 
     def checkthegeneratedpassword(): 
      passwordgenerated1="".join(random.choice("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890!$%^&*()-_=+") for i in range(random.randint(8,12))) 
      point=0 
      contain=1 
      symbol=set("[!$%^&*()-_=+]") 
      point+=len(passwordgenerated1) 
      if re.search("[0-9]", passwordgenerated1): 
       point += 5 
       contain+= 1 
      if re.search("[a-z]", passwordgenerated1): 
       point+=5 
       contain+= 1 
      if re.search("[A-Z]", passwordgenerated1): 
       point+=5 
       contain+= 1 
      if any((c in symbol)for c in passwordgenerated1): 
       point+=5 
       contain+= 1 
      if contain==5: 
       point+=10 
      for line in passwordgenerated1.splitlines(): 
       line=line.strip() 
       if re.search(r'^[a-z]+$',line): 
        continue 
       if re.search(r'^[A-Z]+$',line): 
        continue 
       if re.search(r'^[a-zA-Z]+$',line): 
        point -=5 
      if passwordgenerated1.isdigit(): 
       point -= 5 
      if passwordgenerated1.islower(): 
       point -= 5 
      if passwordgenerated1.isupper(): 
       point -= 5 
      if re.match("^[!$%^&*()-_=+]*$", passwordgenerated1): 
       point -= 5 

      if (point > 20): 
       print(passwordgenerated1) 
      else: 
       checkthegeneratedpassword() 
     checkthegeneratedpassword() 
      #checker for gen 
     print("--------------------------------------------------------------------------------") 
     backfromgentomenu=input("Type \"Back\" to go back to the menu \nor \"Quit\" to quit: ").lower() 
     if backfromgentomenu=="back": 
      menureturn() 
     if backfromgentomenu=="quit": 
      quit 







    if x=="3": 
     print("--------------------------------------------------------------------------------") 
     areyousuresimple=input("Are you sure you wish to quit?").lower() 
     if areyousuresimple=="yes": 
      quit 
     elif areyousuresimple=="no" or areyousuresimple=="back": 
      print("--------------------------------------------------------------------------------") 
      menureturn() 
menureturn() 

ありがとうございます。

(あなたは私も参考になっだろうこれに対する解決策を見つけることができる場所を知っている場合)

+2

ヒント:if条件の中で関数を定義しないでください。 –

+0

'userpword'が定義されています'userpasswordlength()'関数の中で関数にアクセスしようとしています。 – glibdud

+0

^^^^^^私は、あなたが私にそれを打ち負かしました。また、PEPを研究して、あなたのコードフォーマットを改善できるかどうかを見てください。なぜなら、私の、私の、コードの混乱! – AJ123

答えて

0

xは、関数のスコープwitin定義されていません。

あなたの関数を他の場所に定義してください。例えば、小さな関数に

チェック長

def userpasswordlength(userpword): 

    if len(userpword)<=8 : 
     print ("Your password is too short, please enter a longer one (8-12 characters") 
     return False 
    elif len(userpword)>=12 : 
     print ("Your password is too long, please enter a shorter one (8-12 characters") 
     return False 

    return True 

チェック文字

def checktheuserspassword(userpword): 

    # TODO: re.search and things 

    return True 

をあなたのコードを破り、他の関数を呼び出すためにあなたの「メニュー」を実行し

def menureturn(): 
    print("Select one of the following numbers: ") 
    print("1) Check Password")  
    print("2) Generate Password") 
    print("3) Quit") 
    x=input() 
    if x == "1": 
     userpword=input("Please enter a password you wish to be checked: ") 
     points = 0 
     valid_length = False 
     while not valid_length: 
      valid_length = userpasswordlength(userpword) 
      points += 1 

     valid_characters = False 
     while not valid_characters: 
      valid_characters = checktheuserspassword(userpword) 
      points += 1 
関連する問題