1. 程式人生 > >登陸系統,列印 修改。使用者資料

登陸系統,列印 修改。使用者資料

# 在一個檔案裡存多個人的個人資訊,如以下
# 使用者名稱 密碼 年齡 職位 部門
# username password age position department
# alex abc123 24 Engineer IT
# rain [email protected] 25 Teacher Teching


# 1.輸入使用者名稱密碼,正確後登入系統 ,列印
# 1. 修改個人資訊
# 2. 列印個人資訊
# 3. 修改密碼
# 2.每個選項寫一個方法
# 3.登入時輸錯3次退出程式



# 一次行取出所有的檔案內容,並且{'alex': ['alex', 'abc123', '24', 'Engineer', 'IT', '13188888888']}
def pick_up_all_info():
with open('persinfo', 'r', encoding='utf-8') as read_f:
user_dict = {}
for line in read_f:
user_lise = line.strip().split(',')
user_dict[user_lise[0]] = user_lise
return user_dict


# 接收pick_up_info傳來的引數,列印使用者資訊
def print_info(all_user_info,name):
user_info = all_user_info[name]
msg = '''
--------------------
Name: %s
Age: %s
Job: %s
Dept: %s
Phone: %s
--------------------
'''%(user_info[2],
user_info[3],
user_info[4],
user_info[5],
user_info[6])
print(msg)


# 修改資訊
def chang_info_to_file(all_user_info,name):
user_info = all_user_info[name]
msg = '''
1. Name: %s
2. Age: %s
3. Job: %s
4. Dept: %s
5. Phone: %s
'''%(user_info[2],
user_info[3],
user_info[4],
user_info[5],
user_info[6])

msg_dic = {
'1': user_info[2],
'2': user_info[3],
'3': user_info[4],
'4': user_info[5],
'5': user_info[6]
}
print(msg)
while True:
print('當前使用者資訊%s' % (' '.join(user_info)))
chioce = input('請選擇要修改的選項 Q退出:').strip().upper()
if chioce.isalnum():
if chioce != 'Q'and chioce.isdigit():
print('current value:%s' % msg_dic[chioce])
revise_new = input('new value:').strip()
if revise_new:
user_info[int(chioce) + 1] = revise_new
else:
print('不能為空,或全是空格')
elif chioce == 'Q':
print('修改完成')
break
else:
print('輸入有誤請重新輸入')
with open('persinfo','w',encoding='utf-8') as write_f:
for i in all_user_info:
user_str = ','.join(all_user_info[i])
write_f.write(user_str)
write_f.write('\n')


#修改密碼
def chang_pwd(all_user_info,name):
user_info = all_user_info[name]
while True:
print('current password:%s'%user_info[1])
revise_new = input('new password:').strip()
revise_new_two = input('Confirm new password:').strip()
if not revise_new.isspace():
if revise_new == revise_new_two:
all_user_info[name][1] = revise_new
print('修改成功')
break
else:
print('密碼輸入不一致,請重新輸入')
continue
else:
print('不能有空格存在')
continue
with open('persinfo','w',encoding='utf-8') as write_f:
for i in all_user_info:
user_str = ','.join(all_user_info[i])
write_f.write(user_str)
write_f.write('\n')


if __name__ == '__main__':
msg = '''
1:修改個人資訊
2:列印個人資訊
3:修改密碼'''

msg_dic = {
'1': chang_info_to_file,
'2': print_info,
'3': chang_pwd
}
USER_DICT = pick_up_all_info()
name = None
# print(USER_DICT)
counte = 3
while counte > 0:
name = input('username:').strip()
pwd = input('password:').strip()
if name in USER_DICT:
if pwd == USER_DICT[name][1]:
print(('Welcome %s' % name).center(50, '-'))
break
else:
print('User Name or Password Force')
else:
print('User Name does not exist')
counte -= 1
else:
exit('謝謝使用')

while True:
print(msg)
chioce = input('請按編號選擇要做的事情 Q退出:').upper().strip()
if chioce.isdigit():
msg_dic[chioce](USER_DICT, name)
if chioce == 'Q':
exit('謝謝使用')
else:
print('請正確輸入')



#根據name取出指定的,使用者資訊。 上面程式碼用不到。
def pick_up_info(name,pwd):
    # 每次都需要開啟檔案進行判斷,返回對應當前使用者名稱的 所有資訊\
    # {'peiqi': ['peiqi', '456', '26', 'Teacher', 'IT', '13188888888']}
    with open('persinfo', 'r', encoding='utf-8') as read_f:
        user_dict = {}
        tag = False  #用於判斷是否有這個使用者
        for line in read_f:
            user_li = line.strip().split(',')  #對讀取到的字串 去除\n 按 ,分割成列表
            if name in user_li:
                tag = True # 當檔案中有這個使用者時 tag=Ture
                if pwd == user_li[1]:
                    user_dict[user_li[0]] = user_li
                    break
                else:
                    print('賬戶名密碼不對')
        else:
            if not tag:   #當檔案中不存在輸入的使用者名稱時,就執行。
                print('使用者不存在')
        return user_dict