验证密码并更新Python字典



我必须创建一个验证函数来检查我的密码是否满足所有要求:

密码必须至少包含8个字符。-密码必须至少包含一个小写字符。-密码必须至少包含一个大写字符。-密码必须至少包含一个数字。-用户名&密码不能是相同的

def valid(password, username):
isValid = True
if len(password) < 8:
isValid = False
return isValid
elif password == username :
isValid = False
return isValid
elif not any(x.islower() for x in password):
isValid = False
return isValid
elif not any(x.isupper() for x in password):
isValid = False
return isValid
elif not any(x.isdigit() for x in password):
isValid = False
return isValid
elif isValid:
return isValid
username = "Brendon"
password = "ui67SAjjj"
print(valid(password, username))

然后我必须编写我的注册函数,并检查我的用户名是否在我的user_accounts(字典(中。如果不是,我必须:-更新user_accounts字典中的用户名和相应密码。-更新log_in字典,将值设置为False。-返回True。

def signup(user_accounts, log_in, username, password):
if username not in user_accounts.keys():
return True


if valid(password)== password:


user_accounts[username] = password
log_in[username]== False
return True

else:
return False


else:
return False

当我运行代码时,我得到的是:

{}
{}
True

我的dictionary是空的,我认为dictionary.update命令是错误的,因为验证功能正在工作。这两个函数是我打开file.txt的另一个函数的链接。谢谢你的帮助

请检查以下函数。可能这就是你所需要的

def valid(password, username):
if len(password) < 8:
return False
elif password == username :
return False
elif not any(x.islower() for x in password):
return False
elif not any(x.isupper() for x in password):
return False
elif not any(x.isdigit() for x in password):
return False
else:
return True


def signup(user_accounts, log_in, username, password):
# username not there, so unable to signup
if username not in user_accounts.keys():
return False
else:
# If password is not valid, return False
if valid(password):    
# If both username exists and password is valid, you can check if user credentials are valid and continue 
user_accounts[username] = password
log_in[username]== False
return True        
else:
return False

最新更新