-6
Pythonで事前定義された関数を実行した後、私はプログラム全体の機能にとって非常に重要です。それ以外の関数で変数を使用すると、Pythonは定義されていないと言います
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は変数が未定義であると言います。ちなみに、このプログラムは、ユーザが手動で閉じて再オープンする必要なしに、再利用することを潜在的に選択するための機能の中にあります。
ご協力いただき、ありがとうございます。
もっと小さいMCを書く... –