1.作业:
# 做一个注册登录系统
# input("请输入要使用的功能(1-3)")
# 1.注册 (用户输入 账户名 密码 确认密码 如果前后两次密码一直则保存用户的账户和密码到account.txt中)
# 2.登陆 用户输入 账户名 密码 跟account.txt中的账户密码进行比对,如果相同则登录成功
# 3.退出 使用sys模块中方法
slat = "zdihzesiu" def password_md5(p): md5 = hashlib.md5(p.encode("utf-8")) md5.update(slat.encode("utf-8")) return md5.hexdigest() while True: print("*" * 20) print("1.注册") print("2.登录") print("3.退出") print("*" * 20) num = input("请输入你要执行的选项") if num == "1": user_name = input("请输入用户名") password = input("请输入你的密码") re_password = input("请确认你的密码") if password == re_password: dc = {user_name: password_md5(password)} with open('user_passwords.txt', 'w') as file: for user, password in dc.items(): file.write(f"{user_name}:{password}\n") print("注册成功") else: print("密码不一致请从头来过") elif num == "2": user_name_login = input("请输入登录的用户名") password_login = input("请输入密码") password_login_md5 = password_md5(password_login) with (open("user_passwords.txt", "rt") as file): for line in file: user = line.split(':')[0] pwd = line.split(':')[1].replace("\n", "") if user == user_name_login and \ pwd == password_login_md5: print("登录成功!") break else: print("用户名或密码错误!") elif num == "3": print("再见!") sys.exit() else: print("请重新输入一个数")
运行结果
2. 如何判断一个字符串是否是另一个字符串的子串
s1 = input("请输入第一个字符串") s2 = input("请输入第二个字符串") i = s2.find(s1) if i < 0: print(f"{s1}不是{s2}的子串") else: print(f"{s1}是{s2}的子串")
3. 如何验证一个字符串中的每一个字符均在另一个字符串中出现
s1 = input("请输入第一个字符串") s2 = input("请输入第二个字符串") set1 = set(s1) set2 = set(s2) set3 = set1.intersection(set2) if set3 == set1: print(f"{s1}中每个字符出现在了{s2}中") else: print(f"{s1}中不是每个字符出现在了{s2}中")
4 如何判定一个字符串中既有数字又有字母
s1 = input("请输入一个字符串") has_latter = False has_number = False for char in s1: if char.isalpha(): has_latter = True elif char.isdigit(): has_number = True if has_latter and has_number: print("字符串中有数字和字母") else: print("字符串中缺少数字或字母")