编写函数 check(username, password, cypher) 用于检测用户输入2的登录信息,如果用户名和密码都正确则返回 True,否则返回False:
(1) username 为用户名的字符串,长度 4 ~ 12 个字符,只能由英文字母、数字和下划线组成,第一个字符只能是英文字母;
(2) password 为密码的字符串,长度 6 ~ 16 位字符,只能由英文字母、数字和特殊字符“+-*/@#$%”组成;
(3) cypher 为用户名做关键字的字典,对应的密码为键值;
(4) 用户名和密码均区分大小写。在完成函数的基础上实现程序:输入密码文件的全路径+文件名,读入文件后建立字典 cypher,再调用该函数,检验输入的用户名和密码是否正确,当格式与用户名和密码完全正确,显示“登录成功,”否则显示“登录失败”,失败时可以累计尝试输入三次。密码文件为已预存的密码相关信息,每行存放一个用户名与密码,中间用空格分开。
编写函数check(username,password,cypher)用于检测用户输入的登录信息
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
梦幻精灵_cq 2023-11-24 23:48关注- 代码运行效果截屏图片


cypher文本
python代码
from re import findall def check(username, passwords, cypher): if not username[0].lower().isalpha() or not 4 <= len(username) <= 14 or findall(r'[^a-z^A-Z^0-9^_]+', username): print(f"\n{' 用户名格式不对!':~^32}") exit() if not 6 <= len(passwords) <= 16 or findall(r'[^a-z^A-Z^0-9^+^\-^*^/^@^#^$^%]+', username): print(f"\n{' 密码格式不对!':~^33}") exit() if username in cypher.keys() and cypher.get(username) == passwords: print(f"\n{' 登录成功!':~^35}") else: print(f"\n{' 登录失败!':~^35}") if __name__ == '__main__': filename = '/sdcard/Documents/cypher.txt' cypher = {i.split()[0]: i.split()[-1] for i in open(filename).read().split('\n')[1:]} #print(cypher) username = input('\n用户名(4~12位字符):').strip() passwords = input('密码(6~16位字符):').strip() check(username, passwords, cypher)本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报- 代码运行效果截屏图片