天天看點

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('密碼錯誤!')


           

繼續閱讀