def printInfo(StudentData,StudentName):
"""
:param StudentData:
:param StudentName:
:return:
"""
student = StudentData[StudentName]
student_Info = """
----------------------
name: %s
age: %s
sex: %s
no: %s
----------------------
"""%(student[2],student[3],student[4],student[5])
print(student_Info)
def createStudent(StudentData,username,password,name,age,sex,no):
"""
:param StudentData:
:param username:
:param password:
:param name:
:param age:
:param sex:
:param no:
:return:
"""
student = [username,password,name,age,sex,no]
StudentData.setdefault(student[0],student)
f = open('account','r+',encoding='utf8')
f.seek(0)
f.truncate()
for st in StudentData:
row = ','.join(StudentData[st])
f.write('%s\n'%row)
f.flush()
f.close()
def saveFile(StudentData):
"""
:param StudentData:
:return:
"""
f = open('account', 'r+', encoding='utf8')
f.seek(0)
f.truncate()
for st in StudentData:
row = ','.join(StudentData[st])
f.write('%s\n' % row)
f.flush()
f.close()
def readFile():
"""
:return: accouts 从文件将数据提出并转化为dict
"""
f = open('account','r',encoding='utf8')
data = f.readlines()
accounts = {}
for line in data:
line = line.strip()
if not line.startswith('#'):
items = line.split(",")
accounts[items[0]] = items
f.close()
return accounts
def login(StudentData,account,password):
"""
:param StudentData:
:param account:
:param password:
:return:
"""
if account in StudentData.keys():
if password == StudentData[account][1]:
return 1
else:
print("密码输入错误!")
return None
else:
print("不存在该用户!")
return None
def changeInfo(StudentData,account):
columnData = ["Name Age Sex No"]
print(columnData)
for index, i in enumerate(StudentData[account]):
if index>1:
print("%s. %s"%(index-1,i))
if __name__ == "__main__":
menu = """
0.更换账户
1.查看信息
2.修改信息
3.修改密码
4.创建用户(需要管理员密码)
"""
StudentData = readFile()
num = 0
while num < 3:
account = input("account:")
password = input("password:")
num+=1
while login(StudentData,account,password):
print("welcome %s ".center(50, '-') % account)
print(menu)
user_choice = input('>>>').strip()
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice == 1:
printInfo(StudentData, account)
if user_choice == 2:
changeInfo(StudentData, account)
ch = input('>>>').strip()
if ch.isdigit():
ch = int(ch)
for i in range(5):
if ch == i:
print("old:%s"%(StudentData[account][ch+1]))
new = input("new:").strip()
StudentData[account][ch+1]=new
saveFile(StudentData)
break
else:
print("未输入规定范围内数字")
else:
print("你输入的不是数字")
if user_choice == 3:
old = input("请输入旧密码:").strip()
if old == StudentData[account][1]:
new = input("输入新的密码:").strip()
StudentData[account][1] = new
saveFile(StudentData)
print("修改成功!")
else:
print("密码输入错误!")
if user_choice == 4:
print("你正在进行管理员操作...")
admin = input("请输入管理员密码:").strip()
if admin == 'admin':
new_account = input('new_account:')
new_password = input('new_password:')
new_name = input('new_name:')
new_age = input('new_age:')
new_sex = input('new_sex:')
new_no = input('new_no:')
createStudent(StudentData,new_account,new_password,new_name,new_age,new_sex,new_no)
else:
print("密码输入错误!")
if user_choice == 0:
break
print("你输入错误超过3次。。。")
一个Python编写的小程序(学生信息管理系统)
最新推荐文章于 2024-07-22 07:50:17 发布