这是我的交易工作,我打算创建一个程序,生成一个强密码检查用户输入的强度并具有退出选项。我最初在tkinter上做了这个,但遇到了一个问题,所以我决定先把它做成文本,如果我最后留下了几匹马,那么把它转换成tkinter。
我不明白为什么但是我收到了这个错误:
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
我已经尝试了一些东西,但没有任何我希望解决它的东西,说实话,我不明白问题是什么。我在网上寻求帮助,但我只发现了与我的情况太不相同的工作者。(我已经被困在这个问题上差不多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()
感谢您的帮助。
(如果你知道我在哪里可以找到解决方案,也会有所帮助)
答案 0 :(得分:0)
x
未在函数范围内定义。
在别处定义你的功能。例如,将代码分解为更小的函数
检查长度
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