python全栈笔记-day06-function

#修改个人信息程序
#在一个文件里存多个人的个人信息
#要求:
#1.输入用户密码,正确后登陆系统,并打印
####1.修改个人信息
####2.打印个人信息
####3.修改密码
#2.每个选项写一个方法
#3.登陆时输错3次退出程序

lst = []
lst2 = []

def change_info(list):#修改个人信息
    print('''-------Personal Infomation------
1.Username:  %s   
2.Age:       %s   
3.Job:       %s   
4.Department:%s   
-------End------'''%(list[0],list[2],list[3],list[4]))
    change_choice = input('请输入修改项:')
    if change_choice == '1':
        print('原Username:',list[0])
        new_content = input('现Username:')
        isexist = False
        for i in lst2:
            if new_content in i:
                print('已存在!')
                isexist = True
        if not isexist:
            list[0] = new_content
            save_in_file()
            print('修改成功!')
    elif change_choice == '2':
        print('原Age:',list[2])
        new_content = input('现Age:')
        list[2] = new_content
        save_in_file()
        print('修改成功!')
    elif change_choice == '3':
        print('原Job:',list[3])
        new_content = input('现Job:')
        list[3] = new_content
        save_in_file()
        print('修改成功!')
    elif change_choice == '4':
        print('原Department:',list[4])
        new_content = input('现Department:')
        list[4] = new_content
        save_in_file()
        print('修改成功!')
    else:
        print('输入错误!')

def print_info(list):#打印个人信息
    print('Username:%s  Age:%s  Job:%s  Department:%s'%(list[0],list[2],list[3],list[4]))

def change_password(list):
    old_password = input('原密码:')
    if old_password == list[1]:
        new_password = input('新密码:')
        list[1] = new_password
        save_in_file()
    else:
        print('密码错误!')

def save_in_file():
    f = open('函数练习题.txt', 'w')
    for i in lst2:
        f.writelines(','.join(i) + '\n')  # ','.join(i)的作用是将i转化为字符串(相当于将列表的[]去掉)
    f.close()


username = input('Username:')
count = 0#用于计算密码错误次数

#将文件内容读取进内存
f = open('函数练习题.txt','r')
lst = [line.strip() for line in f.readlines()]
f.close()

for i in lst:
    lst2.append(i.split(','))#按逗号将各信息分割成独立的字符串
for i in lst2:
    if username in i:#判断用户名是否存在
        while True:
            password = input('Password:')
            if password == i[1]:#密码正确
                print('Login successfully!')
                while True:
                    print('''1.修改个人信息
2.打印个人信息
3.修改密码''')
                    choice = input('请输入:')
                    if choice == '1':
                        change_info(i)
                    elif choice == '2':
                        print_info(i)
                    elif choice == '3':
                        change_password(i)
                    else:
                        print('输入错误!')
                break
            else:
                count += 1
                if count == 3:
                    print('密码输入错误已达3次!')
                    break
                else:
                    print('密码错误!')


猜你喜欢

转载自blog.csdn.net/weixin_37267713/article/details/82826929