2017-12-26 6 views
-6

Nach der Ausführung einer vordefinierten Funktion in Python, kann ich den zurückgegebenen Wert nicht außerhalb davon verwenden, obwohl es für das Funktionieren meines Programms als Ganzes ziemlich entscheidend ist . Hier ist der Code:Python sagt, es ist nicht definiert

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)") 

Mein Programm sollte das Kennwort des Benutzers und seine Festigkeit prüfen, durch Modifizieren des ‚Score‘ nach jedem bestimmten Kriterium erfüllt ist. Wenn ich jedoch versuche, es außerhalb der Funktion zu verwenden, sagt Python, dass die Variable nicht definiert ist. Übrigens ist das Programm in einer Funktion rein für den Zweck des Benutzers potenziell zu wählen, es wieder zu verwenden, ohne die Notwendigkeit, es manuell zu schließen und wieder zu öffnen.

Ich wäre dankbar für jede Hilfe.

+1

Schreiben Sie eine viel kleinere MCVE ... –

Antwort

-1

Das Problem ist, dass Sie den Wert von Score zurückgeben. Sie erstellen keine Variablenzuweisung, indem Sie nur den Wert von etwas zurückgeben.

Wenn Sie eine Variable auf den zurückgegebenen Wert Ihrer Funktion Registrierungsformular wie folgt setzen: my_score = Registration_form() dann hat my_score den Wert, den Score am Ende der Funktion hatte.

Verwandte Themen