需求:
在一个文件里存多个个人信息,如下 account.txt
shanshan,shanshan,杜姗姗,22,Model,PR,22 alex,abc123,Alexander Li,222,CEO,IT,1333 rain,rain,ysl,27, Engineer ,IT ,133542453453
1.输入用户密码, 正确后登陆系统, 打印
1.修改个人信息 2.打印个人信息 3.修改密码 4.退出(q)
2.每个选项写一个方法
3.登陆是输错3次退出程序
# 打印 文件里面内容
def show_information(username): with open("account.txt","r+",encoding="utf-8") as f: for line in f: user = line.split(',') [0].strip() passwd = line.split(',') [1].strip() name = line.split(',') [2].strip() age = line.split(',') [3].strip() post = line.split(',')[4].strip() #岗位 occupation = line.split(',')[5].strip() #职业 phone= line.split(',')[6].strip() if username == user: print( {"账号:":user, "密码:": passwd, "姓名:": name, "年纪:": age, "岗位:": post, "职业:": occupation, "手机号码:":phone })
#auth 认证 def login(username,password): with open("account.txt","r+",encoding="utf-8") as f: for line in f: user = line.split(',') [0].strip() passwd = line.split(',') [1].strip() # name = line.split(',') [2].strip() # age = line.split(',') [3].strip() # post = line.split(',')[4].strip() #岗位 # occupation = line.split(',')[5].strip() #职业 # phone= line.split(',')[6].strip() if username == user and password==passwd: print(username,password) return [0,username]
# 修改个人信息 def modify_information(username): f_new = open("account_new.txt","w",encoding="utf-8") f_old = open("account.txt","r+",encoding="utf-8") print( "\t2.修改姓名\n" "\t3.修改年纪\n" "\t4.修改岗位\n" "\t5.修改职业\n" "\t6.修改手机号码\n") modify = int(input(" 请输入选项: ").strip()) modify_num = list(range(2, 7)) if modify not in modify_num: return 1 for line in f_old: user = line.split(',')[0].strip() if user == username: infor = input(" 修改为的内容: ").strip() ll = line.split(',') ll[modify] = infor ll = ','.join(ll) f_new.write(ll) else: new_line = line f_new.write(new_line) f_new.close() f_old.close() if os.path.getsize("account_new.txt") !=0: os.replace("account_new.txt","account.txt")
# 修改密码 def modify_passwd(username): f_new = open("account_new.txt", "w", encoding="utf-8") f_old = open("account.txt", "r+", encoding="utf-8") for line in f_old: user = line.split(',')[0].strip() if user == username: while True: infor = input(" 新密码为的内容: ").strip() infor_again = input(" 新密码为的内容: ").strip() if infor == infor_again: line = line.split(',') line[1] = infor line = ','.join(line) f_new.write(line) break else: new_line = line f_new.write(new_line) f_old.close() f_new.close() os.replace("account_new.txt", "account.txt")
# 主函数 def main(): count = 0 while count < 3: user = input("user: ") passwd = input("passwd: ") try: auth = login(user, passwd)[0] if auth==0: print("登录成功") while True: print("\n-----------有以下几个功能选项----------------") select = int(input("\t1.修改个人信息\n" "\t2.打印个人信息\n" "\t3.修改密码\n" "\t4.退出(q)\n" "\n").strip()) if select == 1: modify_information(user) # ("1.修改个人信息") elif select == 2: show_information(user) # ("2.打印个人信息") elif select == 3: modify_passwd(user) # ("3.修改密码") elif select == 4: print("4.退出(q)") exit() else: print("输入有误 重新输入") continue except: print("登录失败。。。。") count +=1 continue