2017-12-26 11 views
-6

파이썬에서 미리 정의 된 함수를 실행 한 후에는 내 프로그램을 전체적으로 작동시키는 데 매우 중요하지만 반환 된 값을 사용할 수는 없습니다. . 여기 코드는 다음과 같습니다마다 특정 기준 후 '점수'를 수정하는 것은 만족을 통해변수 밖에서 함수를 사용하면 파이썬에서 정의되지 않음이라고 말합니다.

import re 
import time 
import getpass 

def Registration_form(): 
    #defines function for checking for presence of extended characters (i.e. ascii 127-255) 
    def hasExtended(s): 
     return any(ord(i) >= 127 for i in s) 
    #defins function for checking for presence of special characters 
    def hasSpecial(s): 
     return any(ord(i) < 48 for i in s) 
    #creates variable responsible for choosing password strength (i.e. weak, medium or strong) 
    Score = 0 
    print("Welcome to the registration form! You need to input your e-mail, username, password and phone number.") 
    Email = input("Please enter your email address:") 
    #checks for presence of at symbol in e-mail 
    while not '@' in Email: 
     print("Please enter a real e-mail address!") 
     Email = input("Please enter your email adress:") 
    else: 
     Username = str(input("Please enter your username:")) 
     Username_length = len(Username) 
     Extended_presence = hasExtended(Username) 
     Special_presence = hasSpecial(Username) 
     #checks username length (4-15) and presence of extended or special characters, asks user to re-input until within boundaries 
     while Username_length < 4 or Username_length > 15 or Extended_presence or Special_presence: 
      if Username_length < 4: 
       First_criterion = "is too short" 
       Second_criterion = "be no shorter than 4 characters" 
      elif Username_length > 15: 
       First_criterion = "is too long" 
       Second_criterion = "be no longer than 15 characters" 
      elif Extended_presence: 
       First_criterion = "contains extended characters" 
       Second_criterion = "only contain numbers and alphabetical characters" 
      elif Special_presence: 
       First_criterion = "contains special characters" 
       Second_criterion = "only contain numbers and alphabetical characters" 
      print("Your password", First_criterion, ". It has to", Second_criterion, ".") 
      Username = str(input("Please enter your username:")) 
      Username_length = len(Username) 
      Extended_presence = hasExtended(Username) 
      Special_presence = hasSpecial(Username) 
     else: 
      #inputs password 
      Password = input("Please enter your password:") 
      Password_length = len(Password) 
      Extended_presence = hasExtended(Password) 
      #checks password length (6-12), asks user to re-input until within boundaries 
      #checks if password contains extended characters 
      while Password_length < 6 or Password_length > 12 or Extended_presence: 
       if Password_length < 6: 
        First_criterion = "is too short" 
        Second_criterion = "be no shorter than 6 characters" 
       elif Password_length > 12: 
        First_criterion = "is too long" 
        Second_criterion = "be no longer than 12 characters" 
       elif Extended_presence: 
        First_criterion = "contains extended characters" 
        Second_criterion = "only contain alphabetical, number and special characters" 
       print("Your password", First_criterion, ". It has to", Second_criterion, ".") 
       Password = input("Please input your password:") 
       Password_length = len(Password) 
       Extended_presence = hasExtended(Password) 
      else: 
       #defines function for checking for presence of numbers 
       def hasNumbers(s): 
        return any(i.isdigit() for i in s) 
       #defines function for checking for presence of letters 
       def hasLetters(s): 
        return any(i.isalpha() for i in s) 
       #checks if password contains letters 
       Letter_presence = hasLetters(Password) 
       if not Letter_presence: 
        Score = Score - 1 
        Improvements.append("letters") 
       else: 
        Score = Score + 1 
       #checks if password is all upper case 
       Is_upper = Password.isupper() 
       if not Is_upper: 
        Score = Score + 1 
       else: 
        Score = Score - 1 
        Improvements.append("upper and lower case letters") 
       #checks if password is all lower case 
       Is_lower = Password.islower() 
       if not Is_lower: 
        Score = Score +1 
       else: 
        Score = Score - 1 
        Improvements.append("upper and lower case letters") 
       #checks if password contains a number 
       Number_presence = hasNumbers(Password) 
       if not Number_presence: 
        Score = Score + 0 
        Improvements.append("numbers") 
       else: 
        Score = Score + 1 
       #checks if password is just numbers 
       Only_numbers = Password.isdigit() 
       if not Only_numbers: 
        Score = Score + 0 
       else: 
        Score = Score - 1 
        Improvements.append("other characters") 
       #checks if password contains special characters 
       Special_presence = hasSpecial(Password) 
       if not Special_presence: 
        Score = Score + 0 
        Improvements.append("special characters such as $") 
       else: 
        Score = Score + 1 
       #outputs weak, medium or strong password to user and suggests improvements 
       if Score <= 3: 
        print("The program is processing your password...") 
        time.sleep(2) 
        print("Your password is weak! Please try again.") 
        print("Next time, remember to include", Improvements) 
        time.sleep(5) 
        Registration_form() 
       elif Score == 4: 
        print("The program is processing your password...") 
        time.sleep(2) 
        print("Your password is medium, it should be OK.") 
        print("Next time, remember to include", Improvements) 
       elif Score == 5: 
        print("The program is processing your password...") 
        time.sleep(2) 
        print("Your password is strong, it is absolutely fine.") 
    return Score 

Registration_form() 

#user decision to re-use form or exit if password is medium or strong 
if Score >= 4: 
    time.sleep(1) 
    #sets random value for variable so while loop can work initially 
    Decision = "yes" 
    while Decision == "no" or Decision == "No" or Decision == "yes" or Decision == "Yes": 
     Decision = input("Would you like re-use this registration form? (Yes/No): ") 
     if Decision == "no" or Decision == "No": 
      print("Thank your for using our registration form! We hope it was useful to you!") 
      print("The program will terminate in 5 seconds.") 
      time.sleep(5) 
      exit() 
     elif Decision == "yes" or Decision == "Yes": 
      Registration_form() 
    else: 
     print("You have entered an invalid value. Please try again and remember to enter a Yes or No answer!") 
     Decision = input("Would you like to re-use this registration form? (Yes/No)") 

내 프로그램은 사용자의 암호와 강도를 확인하기위한 것입니다. 그러나 함수 밖에서 사용하려고하면 Python에서는 변수가 정의되지 않았다고 말합니다. 그런데이 프로그램은 수동으로 닫고 다시 열 필요없이 잠재적으로 사용자가 재사용 할 수 있도록하기위한 용도로만 사용할 수 있습니다.

도움에 감사드립니다.

+1

더 작은 MCU 쓰기 ... –

답변

-1

문제는 점수 값을 반환한다는 것입니다. 변수의 값을 반환하는 것만으로 변수 할당을 작성하지 않습니다.

함수의 반환 값으로 변수를 설정 한 경우 다음과 같이 my_score는 Score에 함수 끝에 값이 있습니다.