一个练习题,本人又做了一些功能的增加和完善。题目是:个人信息修改小程序。
IDE: Pycharm
OS: mac
功能需求:
1.输入管理员和密码,正确后登录系统,打印:
1. 打印个人信息
2. 修改个人信息
3. 修改密码
4. 添加个人信息
源码如下:
# 个人信息修改,查询小程序
def print_personal_info(account_dic, username):
"""
print user info
:param account_dic: all account's data
:param username: username
:return: None
"""
person_data = account_dic[username]
info = '''
------------------------
Name: %s
Age: %s
Job: %s
Dept: %s
Phone: %s
-------------------------
'''%(person_data[0],
person_data[2],
person_data[3],
person_data[4],
person_data[5],
)
print(info)
def save_back_to_file(account_dic):
"""
把account_dic 转成字符串格式, 写回文件
:param account_dic:
:return:
"""
with open('account.txt', mode='a', encoding='utf-8') as f:
f.seek(0) # 回到文件头
f.truncate() # 清空原文件
for k in account_dic:
row = ','.join(account_dic[k])
f.write('%s\n'%row)
f.flush()
def change_personal_info(account_dic, username):
"""
change user info,思路如下
1.把这个人的每个信息打印出来,让其选择改哪个字段,用户选择了的数字,正好是字段的索引,这样直接把字段找出来改掉就可以了
2.改完后,还要把这个新数据重新写回到account.txt,由于改完后的新数据是dict类型,还需把dict转成字符串后,再写回硬盘
:param account_dic:all account's data
:param username:username
:return:None
"""
person_data = account_dic[username]
# print('person data:', person_data)
column_names = ['Name', 'Password', 'Age', 'Job', 'Dept', 'Phone']
print('1. ' + person_data[0])
for index, k in enumerate(person_data):
if index > 1: #0 is username and 1 is password
print('%s. %s:%s'%(index, column_names[index],k))
choice = input('[select column id to change]:').strip()
if choice.isdigit():
choice = int(choice)
if choice > 0 and choice < len(person_data): # index不能超出列表长度边界
column_data = person_data[choice] # 拿到修改的数据
print('current value>:', column_data)
new_val = input('new value>:').strip()
if new_val: # 不能为空
person_data[choice] = new_val
print(person_data)
save_back_to_file(account_dic) # 改完写回文件
else:
print('不能为空....')
# 修改密码
def change_password(account_dic, username):
"""
修改个人密码
:param account_dic: 用户信息字典
:param username: 用户名
:return: None
"""
new_password = input('new password>:').strip()
if new_password: # 不能为空
account_dic[username][1] = new_password
save_back_to_file(account_dic) # 改完写回文件
else:
print('不能为空....')
def add_personal_info(account_dic, username):
"""
增加个人信息
:param account_dic: 用户信息字典
:return: None
"""
# 要添加的个人信息
password = input('请输入密码:')
age = input('请输入年龄:')
position = input('请输入工作:')
depart = input('请输入部门')
phone = input('请输入电话:')
info = '''
------------------------
Name: %s,
Password: %s,
Age: %s,
Job: %s,
Dept: %s,
Phone: %s
-------------------------
'''%(username, password, age, position, depart, phone)
print(info)
account_dic[username] = [username, password, age, position, depart, phone]
save_back_to_file(account_dic)
# 便于查询操作,把文件变成字典
account_file = 'account.txt'
accounts = {
}
# 把账户数据从文件里读出来,变成dict,这样后面就好查询了
with open(account_file, 'r+', encoding='utf-8') as f:
raw_data = f.readlines() # 读取所有的行数据,列表形式
for line in raw_data:
line = line.strip()
if not line.startswith('#'):
items = line.split(',') # 行数据变成列表
accounts[items[0]] = items # 用首个元素name当作accounts字典的键,行数据列表变成值
# 主界面
menu = '''
1. 打印个人信息
2. 修改个人信息
3. 修改密码
4. 添加个人信息
'''
count = 0
while count < 3:
username = input('Administrator:').strip()
password = input('Password:').strip()
if username in accounts:
if password == accounts[username][1]: # 字典的值的第2项是密码
print('welcome %s'.center(50, '-') %username)
while True:
print(menu)
user_choice = input('>>>').strip()
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice == 1:
print_personal_info(accounts, username)
elif user_choice == 2:
change_personal_info(accounts, username)
elif user_choice == 3:
change_password(accounts, username)
elif user_choice == 4:
username = input('请输入姓名:')
add_personal_info(accounts, username)
elif user_choice == 'q':
exit('bye.')
else:
print('Wrong username or password!')
else:
print('Username does not exist!')
count += 1
else:
print('too many attempts')
运行结果如下图: