python学习day7

Posted dingyang-python

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习day7相关的知识,希望对你有一定的参考价值。

import json
import sys


status_dict={
‘username‘:None,
‘status‘:False,
‘id‘:None
}



def login(): #登陆函数
i = 0
while i < 3:
print("校园管理系统".center(50,‘*‘))
print("请登录!")
username = input(‘请输入用户名:‘).strip()
password = input(‘请输入密码:‘).strip()
if status_dict["username"] is not None and status_dict["status"] == True:
print("用户"+status_dict["username"]+"已登陆,请注销后重新登陆。")
break
with open(‘userinfo‘,encoding=‘utf-8‘) as f1:
for line in f1:
line_list = line.strip().replace(‘,‘, ‘,‘).split(‘,‘)
if username == line_list[0] and password==line_list[1]:
print(‘登录成功...‘)
status_dict[‘username‘] = username
status_dict[‘status‘] = True
status_dict[‘id‘] = line_list[2]
return True
else:
print(‘账号或者密码不正确,请重新输入‘)
i += 1
if i == 3:
exit()

def regiter(*args,**kwargs): #注册函数
while True:
username = input(‘请输入注册用户名:‘).strip()
password = input(‘请输入用户名密码:‘).strip()
idd = input(‘请输入用户身份:‘).strip()
with open(‘userinfo‘,encoding=‘utf-8‘) as f1:
for line in f1:
line_list = line.strip().replace(‘,‘, ‘,‘).split(‘,‘)
if username == line_list[0]:
print(‘用户名存在,请重新输入!‘)
break
else:
with open(‘userinfo‘,encoding=‘utf-8‘,mode=‘a‘) as f2:
f2.write(‘ {},{}‘.format(username,password,idd))
print(‘注册成功‘)
login()


##学校类定义
class School(object):
##初始化函数
def __init__(self, SchoolName, SchoolPlace, SchoolMotto):
self.SchoolName = SchoolName
self.SchoolPlace = SchoolPlace
self.SchoolMotto = SchoolMotto

##招生函数
def RecruitStu(self):
NewStuName = input("StuName:")
NewStuAge = input("StuAge:")
NewStuSex = input("StuSex:")
NewStuSchool = input("StuSchool:")
CourseNo = input("CourseNo:")
NewStuID = input("StuID:")
CoursePrice = input("CoursePrice:")
NewStu = Stu(NewStuName, NewStuAge, NewStuSex, NewStuSchool, CourseNo, NewStuID, CoursePrice) ##实例化学员对象
stus_dict = {"StuName": NewStuName, "StuAge": NewStuAge, "StuSex": NewStuSex, "StuSchool": NewStuSchool,
"CourseNo": CourseNo, "StuID": NewStuID, "CoursePrice": CoursePrice} # 用字典来存放讲师信息
if not dic:
dic[NewStuName] = stus_dict
json.dump(dic, open("student_db", "w"), encoding=‘utf-8‘, ensure_ascii=False, indent=2)
else:
if dic.get(NewStuName):
print("%s学生已存在,不能注册名字相同的学生" % NewStuName)
else:
dic[NewStuName] = stus_dict
json.dump(dic, open("student_db", "w"), encoding=‘utf-8‘, ensure_ascii=False, indent=2)
print("The student has already added successfully".center(50, ‘-‘))
NewStu.StuInfo()

##聘请讲师
def HireTch(self):
print("Welcome to hire teacher from here~")
NewTeacherName = input("Please input the teacher‘s name:")
NewTeacherAge = input("Please input the teacher‘s age:")
NewTeacherSex = input("Please input the teacher‘s sex:")
NewCourseNo = input("Please input the teacher‘s course number:")
NewSalary = input("Please input the teacher‘s salary:")
NewTeacher = Teacher(NewTeacherName, NewTeacherAge, NewTeacherSex, NewCourseNo, NewSalary) # 实例化讲师对象
teachers_dict = {"TeacherName": NewTeacherName, "TeacherAge": NewTeacherAge, "TeacherSex": NewTeacherSex,
"CourseNo": NewCourseNo, "Salary": NewSalary} # 用字典来存放讲师信息
# 通过json将讲师的字典反序列化到dic字典中
if not dic: # 字典如果为空
dic[NewTeacherName] = teachers_dict # 将讲师名与讲师对象相关联
# 通过json将讲师的字典序列化到teacher_db文件中
json.dump(dic, open("teacher_db", "w"), encoding=‘utf-8‘, ensure_ascii=False, indent=2)
else: # 如果文件中已有讲师信息
if dic.get(NewTeacherName): # 字典中不存在key,则返回none,不报错
print("%s讲师已存在,不能创建名字相同的讲师" % NewTeacherName)
else:
dic[NewTeacherName] = teachers_dict
json.dump(dic, open("teacher_db", "w"), encoding=‘utf-8‘, ensure_ascii=False, indent=2)
NewTeacher.TeacherInfo() ##将新老师的信息打印在屏幕上

##新增课程
def CreateCourse(self):
print("Welcome to create course!")
NewCourseNo = input("CourseNo:")
NewCourseName = input("CourseName:")
NewCoursePrice = input("CoursePrice:")
NewCourse = Course(NewCourseNo, NewCourseName, NewCoursePrice) ##新创建的对象直接实例化
courses_dict = {"CourseNo": NewCourseNo, "CourseName": NewCourseName,
"CoursePrice": NewCoursePrice} # 用字典来存放讲师信息
if not dic:
dic[NewCourseNo] = courses_dict
json.dump(dic, open("course_db", "w"), encoding=‘utf-8‘, ensure_ascii=False, indent=2)
else:
if dic.get(NewCourseNo):
print("%s课程已存在,不能注册课程号相同的课程" % NewCourseNo)
else:
dic[NewCourseNo] = courses_dict
json.dump(dic, open("course_db", "w"), encoding=‘utf-8‘, ensure_ascii=False, indent=2)
print("Course has already created successfully".center(50, ‘-‘))
print("""
CourseNo:%s
CourseName:%s
CoursePrice:%s""" % (NewCourseNo, NewCourseName, NewCoursePrice))


class Teacher(object):
def __init__(self, TeacherName, TeacherAge, TeacherSex, CourseNo, Salary):
self.TeacherName = TeacherName
self.TeacherAge = TeacherAge
self.TeacherSex = TeacherSex
self.CourseNo = CourseNo
self.Salary = Salary

def TeachKnowledge(self):
print("Teach Knowlege~")

def TeacherInfo(self):
print("""
-------------讲师信息-------------
Name:%s
Age:%s
Sex:%s
CourseNo:%s
Salary:%s
""" % (self.TeacherName, self.TeacherAge, self.TeacherSex, self.CourseNo, self.Salary))


##课程类定义
class Course(object):
def __init__(self, CourseNo, CourseName, CoursePrice):
self.CourseNo = CourseNo
self.CourseName = CourseName
self.CoursePrice = CoursePrice

def ShowCourseInfo(self):
print("""
CourseNO:%s
CourseName:%s
CoursePrice:%s""" % (self.CourseNo, self.CourseName, self.CoursePrice))


##学员类定义
class Stu(object):
def __init__(self, StuName, StuAge, StuSex, StuSchool, CourseNo, StuID, CoursePrice):
self.StuName = StuName
self.StuAge = StuAge
self.StuSex = StuSex
self.StuSchool = StuSchool
self.CourseNo = CourseNo
self.StuID = StuID
self.CoursePrice = CoursePrice

def Study(self):
print("study")

def PayMoney(self):
print("Paying for money~")

def StuInfo(self):
print("""
---------------学生信息--------------
Name:%s
Age:%s
Sex:%s
School:%s
CourseNo:%s
ID:%s
CoursePrice:%s
""" % (
self.StuName, self.StuAge, self.StuSex, self.StuSchool, self.CourseNo, self.StuID, self.CoursePrice))


def students_view(): # 学员视图
while True:
pro = """
1.欢迎注册
2.返回
3.退出

请选择:
"""
num = input(pro)
if num == ‘1‘:
choice_school_obj.RecruitStu() # 调用学生注册方法并生成学生对象
elif num == ‘2‘:
break
elif num == ‘3‘:
sys.exit()
else:
continue


def teacher_view(): # 讲师视图
name = input("请输入讲师姓名:")
while True:
if dic.get(name) or teachers_dict.get(name):
print("欢迎%s讲师".center(50, ‘-‘) % name)
elif not dic.get(name) and not teachers_dict.get(name):
print("%s讲师不存在" % name)
break
pro = """
1.查看学员信息
2.返回
3.退出

请输入你的选择:
"""
num = input(pro)
if num == ‘1‘:
if teachers_dict.get(name):
teachers_dict[name].show_student() # 查看学生信息
else:
print("功能未完善,只能输入lvah,cheng")
elif num == ‘2‘:
break
elif num == ‘3‘:
sys.exit()
else:
continue


def school_view(): # 学校视图
while True:
pro = """
1.创建课程
2.招生注册
3.聘用讲师
4.返回
5.退出

请输入你的选择:
"""
num = input(pro)
if num == ‘1‘:
choice_school_obj.CreateCourse()
elif num == ‘2‘:
choice_school_obj.RecruitStu()
elif num == ‘3‘:
choice_school_obj.HireTch()
elif num == ‘4‘:
break
elif num == ‘5‘:
sys.exit()
else:
continue


def main():
global dic # 全局变量
global choice_school_obj
dic = {}

while True:

prola = """
1.登录
2.注册
请选择您的需求序号:
"""

choise = input(prola)
if choise == ‘1‘:
login()
elif choise == ‘2‘:
regiter()
print("请选择学校".center(50, ‘*‘))
pro1 = """
1. %s
2. %s
3. 返回
4. 退出

请输入你的选择:
""" % (school1.SchoolName, school2.SchoolName)
choice_school = input(pro1)
if choice_school == ‘1‘:
choice_school_obj = school1 # 将对象引用传给choice_school
elif choice_school == ‘2‘:
choice_school_obj = school2
elif choice_school == ‘3‘:
break
elif choice_school == ‘4‘:
sys.exit()
else:
continue
while True:
print("请选择视图".center(50, ‘*‘))
pro2 = """
1.学员视图
2.讲师视图
3.管理员视图
4.返回
5.退出

请选择视图:
"""

num = input(pro2)

if num == ‘1‘:
if status_dict[‘id‘] == ‘student‘:#做登录账号判断
print("欢迎进入学员视图".center(50, ‘*‘))
students_view()
else:
print("您登录的用户不正确请重新登录~")
login()
elif num == ‘2‘:
if status_dict[‘id‘] == ‘teacher‘:
print("欢迎进入讲师视图".center(50, ‘*‘))
teacher_view()
else:
print("您登录的用户不正确请重新登录~")
login()
elif num == ‘3‘:
if status_dict[‘id‘] == ‘manager‘:
print("欢迎进入学校管理视图".center(50, ‘*‘))
school_view()
else:
print("您登录的用户不正确请重新登录~")
login()
elif num == ‘4‘:
break
elif num == ‘5‘:
sys.exit()
else:
continue


if __name__ == ‘__main__‘:
teachers_dict = {}
courses_dict = {}
stus_dict = {}

school1 = School("SH老男孩", "上海校区", "祖国、荣誉、责任") # 实例化两个学校
school2 = School("BJ老男孩", "北京校区", "爱国、求是、奋进")

t1 = Teacher("alex", "28", "male", "01", "10000")
t2 = Teacher("太白", "26", "female", "02", "9000") # 实例化两个讲师
teachers_dict["alex"] = t1
teachers_dict["太白"] = t2
teacher_dict = {"TeacherName": "alex", "TeacherAge": "28", "TeacherSex": "male", "CourseNo": "01", "Salary": "10000"}
teacher_dict = {"TeacherName": "太白", "TeacherAge": "26", "TeacherSex": "female", "CourseNo": "02",
"Salary": "9000"}

course1 = Course("01", "Linux云自动化运维", "12800") # 实例化两个课程
course2 = Course("02", "python", "9800")
courses_dict["01"] = course1
courses_dict["02"] = course2
courses_dict = {"CourseNo": "01", "CourseName": "Linux云自动化运维", "CoursePrice": "12800"}
courses_dict = {"CourseNo": "02", "CourseName": "python", "CoursePrice": "9800"}

stu1 = Stu("Katy", "18", "female", "alex", "01", "3150911026", "12800") ##实例化两个学员
stu2 = Stu("Betty", "18", "male", "太白", "02", "3150911022", "12000")
stus_dict["Katy"] = stu1
stus_dict["Betty"] = stu2
stu_dict = {"StuName": "静香", "StuAge": "18", "StuSex": "女", "StuSchool": "SH老男孩", "CourseNo": "01",
"StuID": "3150911026", "CoursePrice": "12800"}
stu_dict = {"StuName": "大雄", "StuAge": "18", "StuSex": "男", "StuSchool": "BJ老男孩", "CourseNo": "02",
"StuID": "3150911022", "CoursePrice": "12000"}
print(school1, school2)
main()










































































































































































































































































































































以上是关于python学习day7的主要内容,如果未能解决你的问题,请参考以下文章

Python学习总结

跨行业如何学习好python?Python学习!

AI测试学习Python学习Python基础学习

AI测试学习Python学习Python基础学习

怎么学习Python,学习Python需要那些学习条件?

0基础该不该学习Python?适合学习吗?