一、作业需求
二、流程图
三、源码与具体思路
import shutil import os import sys USER_LOGIN = {'is_login': False} def outer(func): #装饰器,若没有登陆则输出“请登陆” def inner(*args, **kwargs): if USER_LOGIN['is_login']: r = func(*args, **kwargs) return r else: print("请登陆") return inner def outer1(func): #装饰器,若不是管理员则输出“权限不足” def inner1(*args, **kwargs): if USER_LOGIN['user_type'] == '2': r = func(*args, **kwargs) return r else: print("不是管理员,权限不足") return inner1 @outer def change_pwd(changed_user, changed_pwd, type_user): #修改密码 if type_user == '1': print("欢迎%s修改密码".center(50, '-') % USER_LOGIN['current_user']) with open('regist', 'r', encoding='utf-8') as f1, open('regist_new', 'w', encoding='utf-8') as f2: for line in f1: read_list = line.strip().split('|') if read_list[0] == changed_user: read_list[1] = changed_pwd f2.write('|'.join(read_list) + 'n') continue f2.write(line) shutil.move('regist', 'regist_bak') #把regist文件复制到regist_bak文件中去 os.rename('regist_new', 'regist') #再将regist_new改名为regist 妙妙妙! else: print("没有权限修改管理员密码") @outer def look_information(): #查看本用户信息 print("欢迎%s查看信息".center(50, '-') % USER_LOGIN['current_user']) with open('regist', 'r+', encoding='utf-8') as f: for line in f: read_list = line.strip().split('|') if read_list[0] == USER_LOGIN['current_user']: print("用户名:%s" % read_list[0]) print("密码:%s" % read_list[1]) print("邮箱:%s" % read_list[2]) print("电话:%s" % read_list[3]) def login(user, pwd): #登陆 with open('regist', 'r+', encoding='utf-8') as f: for line in f: read_list = line.strip().split('|') if read_list[0] == user and read_list[1] == pwd: #若用户存在 USER_LOGIN['is_login'] = True USER_LOGIN['current_user'] = user USER_LOGIN['user_type'] = read_list[-1] print("欢迎%s登陆".center(50, '-') % USER_LOGIN['current_user']) break if not USER_LOGIN['is_login']: #若用户还没注册 print("用户不存在,请注册") def register(reg_user, reg_pwd, reg_email, reg_phone): #注册 register_list = [] register_list.append(reg_user) #将用户信息加入列表 register_list.append(reg_pwd) register_list.append(reg_email) register_list.append(reg_phone) register_list.append('1') with open('regist', 'a', encoding='utf-8') as f: #将用户信息写入文件 li = '|'.join(register_list) f.write(li + 'n') print("注册/添加信息:", li) def delete_func(dele_user, type_user): #删除普通用户 if type_user == '1': #若删除的是普通用户 exit_flag = False with open('regist', 'r+', encoding='utf-8') as f1, open('regist_new', 'w', encoding='utf-8') as f2: for line in f1: ret_list = line.strip().split('|') if ret_list[0] == dele_user: exit_flag = True #标志位原本设为False,当找到相应用户删除后,标志位设为True print("普通用户删除成功") continue f2.write(line) shutil.move('regist', 'regist_bak') os.rename('regist_new', 'regist') if not exit_flag: print("要删除普通用户不存在") elif type_user == '2': #若删除的是管理员用户 print("没有权限删除管理员帐号") def upper_level(upper_user): #升级为管理员 with open('regist', 'r+', encoding='utf-8') as f1, open('regist_new', 'w', encoding='utf-8') as f2: for line in f1: li = line.strip().split('|') if li[0] == upper_user: li[-1] = '2' print("%s已成为管理员" % upper_user) f2.write('|'.join(li) + 'n') continue f2.write(line) shutil.move('regist', 'regist_bak') os.rename('regist_new', 'regist') def search(search_info): #简单搜索 with open('regist', 'r+', encoding='utf-8') as f: for line in f: ret_list = line.strip().split('|') if search_info in ret_list: print(ret_list) def get_usertype(user): #通地用户名获得用户类型,返回1或2 with open('regist', 'r+', encoding='utf-8') as f: for line in f: li = line.strip().split('|') if li[0] == user: return li[-1] #返回帐号的类型 1或2 @outer1 def admin_user(): #管理员调用 num = input("请选择:1.修改密码;2.查看本用户信息;3.修改普通用户密码;" "4.删除/添加普通用户;5.权限管理;6.关键字搜索普通用户信息;7.退出:") if num == '1': new_pwd = input("请输入新的密码:") change_pwd(new_pwd) elif num == '2': look_information() elif num == '3': user_changed = input("请输入修改密码的用户名:") type_user = get_usertype(user_changed) new_pwd = input("请输入新的密码:") change_pwd(user_changed, new_pwd, type_user) elif num == '4': add_or_dele = input("1.删除普通用户;2.添加普通用户") if add_or_dele == '1': delete_username = input("请输入要删除普通用户的用户名:") type_user = get_usertype(delete_username) delete_func(delete_username, type_user) elif add_or_dele == '2': regi_username = input("请输入注册用户名:") regi_pwd = input("请输入注册密码:") regi_email = input("请输入邮箱:") regi_phone = input("请输入电话:") register(regi_username, regi_pwd, regi_email, regi_phone) elif num == '5': upper_user = input("请输入升级为管理员的普通用户名:") upper_level(upper_user) elif num == '6': search_information = input("请输入要查找的关键字:") search(search_information) elif num == '7': sys.exit() def main(): while True: choice = input("请选择:1.登陆;2.注册;3.修改密码;4.查看信息;5.后台管理;6.退出:") if choice == '1': username = input("请输入用名名:") password = input("请输入密码:") login(username, password) elif choice == '2': regi_username = input("请输入注册用户名:") regi_pwd = input("请输入注册密码:") regi_email = input("请输入邮箱:") regi_phone = input("请输入电话:") register(regi_username, regi_pwd,regi_email,regi_phone) print("注册成功".center(50, '*')) elif choice == '3': new_pwd = input("请输入新的密码:") current_user = USER_LOGIN['current_user'] change_pwd(current_user, new_pwd) print("修改密码成功,请重新登陆".center(50, '-')) sys.exit() elif choice == '4': look_information() elif choice == '5': admin_user() elif choice == '6': sys.exit() main()
为了方便,再贴下不折叠代码
@outer1 def admin_user(): #管理员调用 num = input("请选择:1.修改密码;2.查看本用户信息;3.修改普通用户密码;" "4.删除/添加普通用户;5.权限管理;6.关键字搜索普通用户信息;7.退出:") if num == '1': new_pwd = input("请输入新的密码:") change_pwd(new_pwd) elif num == '2': look_information() elif num == '3': user_changed = input("请输入修改密码的用户名:") type_user = get_usertype(user_changed) new_pwd = input("请输入新的密码:") change_pwd(user_changed, new_pwd, type_user) elif num == '4': add_or_dele = input("1.删除普通用户;2.添加普通用户") if add_or_dele == '1': delete_username = input("请输入要删除普通用户的用户名:") type_user = get_usertype(delete_username) delete_func(delete_username, type_user) elif add_or_dele == '2': regi_username = input("请输入注册用户名:") regi_pwd = input("请输入注册密码:") regi_email = input("请输入邮箱:") regi_phone = input("请输入电话:") register(regi_username, regi_pwd, regi_email, regi_phone) elif num == '5': upper_user = input("请输入升级为管理员的普通用户名:") upper_level(upper_user) elif num == '6': search_information = input("请输入要查找的关键字:") search(search_information) elif num == '7': sys.exit() def main(): while True: choice = input("请选择:1.登陆;2.注册;3.修改密码;4.查看信息;5.后台管理;6.退出:") if choice == '1': username = input("请输入用名名:") password = input("请输入密码:") login(username, password) elif choice == '2': regi_username = input("请输入注册用户名:") regi_pwd = input("请输入注册密码:") regi_email = input("请输入邮箱:") regi_phone = input("请输入电话:") register(regi_username, regi_pwd,regi_email,regi_phone) print("注册成功".center(50, '*')) elif choice == '3': new_pwd = input("请输入新的密码:") current_user = USER_LOGIN['current_user'] change_pwd(current_user, new_pwd) print("修改密码成功,请重新登陆".center(50, '-')) sys.exit() elif choice == '4': look_information() elif choice == '5': admin_user() elif choice == '6': sys.exit() main()
四、坑总结
1.怎么修改密码?
当时我蒙了。因为如果用f.write(xx),是直接写在文件的最后面的。这样我想,好吧写在最后面也行。
但将已修改密码的记录加在最后一行,那又怎么把原来的删掉?又多了一个麻烦。
或者说,不加在最后一行,直接改那一行,又怎么做?
shutil.move('regist', 'regist_bak') #把regist文件复制到regist_bak文件中去 os.rename('regist_new', 'regist') #再将regist_new改名为regist 妙妙妙!
2.怎么修改用户user_type(将1改为2)
我看到有的博客有replace()方法。
new_str = line.replace(read_list[1], changed_pwd) 刚开始我也用了,但是,后来有BUG!
比如:当用户名与密码一样是,修改密码时,把密码也修改了。还不止,用replace()方法,在改
user_type时也出现把用户名等修改的现象。当时我再次蒙比。这不科学!
后来仔细看了replace()的源码才发现我错了。
也可以看看Python replace()方法
3.当我登入管理员后,添加普通用户后,发现不能再次登入管理员了。
于是我回去看register(reg_user, reg_pwd, reg_email, reg_phone),
发现我滥用全局变量
USER_LOGIN['user_type'] = '1' register_list.append(USER_LOGIN['user_type'])
原来这里我重新赋值USER_LOGIN['user_type'] = '1'
后来为了解决这类问题,写了个get_user_type()方法
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。