Python Learning Day7

Source: Internet
Author: User

Import JSON
Import Sys


status_dict={
' username ': None,
' Status ': False,
' ID ': None
}



def login (): #登陆函数
i = 0
While I < 3:
Print ("Campus management System". Center (50, ' * '))
Print ("Please login! ")
Username = input (' Please enter user name: '). Strip ()
Password = input (' Please enter password: '). Strip ()
If status_dict["username"] is not None and status_dict["status"] = = True:
Print ("User" +status_dict["username"]+ "is logged in, please log off and re-login. ")
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 (' Login successful ... ')
status_dict[' username '] = Username
status_dict[' status '] = True
status_dict[' id '] = line_list[2]
Return True
Else
Print (' username or password is incorrect, please re-enter ')
i + = 1
if i = = 3:
Exit ()

def regiter (*args,**kwargs): #注册函数
While True:
Username = input (' Please enter a registered user name: '). Strip ()
Password = input (' Enter user name password: '). Strip ()
IDD = input (' Please enter User id: '). 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 (' username exists, please re-enter! ')
Break
Else
With open (' UserInfo ', encoding= ' utf-8 ', mode= ' a ') as F2:
F2.write (' \n{},{} '. Format (Username,password,idd))
Print (' Registration successful ')
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} # Store instructor information in a dictionary
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 student already exists, cannot enroll student with the same name"% 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) # Instantiate Instructor Object
Teachers_dict = {"TeacherName": Newteachername, "teacherage": Newteacherage, "Teachersex": Newteachersex,
"Courseno": Newcourseno, "Salary": newsalary} # Store instructor information in a dictionary
# Deserialize the instructor's dictionary into the DIC dictionary via JSON
If not dic: # dictionary if empty
Dic[newteachername] = teachers_dict # Associate Instructor name with instructor Object
# serializing the instructor's dictionary to the teacher_db file via JSON
Json.dump (DIC, open ("teacher_db", "W"), encoding= ' Utf-8 ', Ensure_ascii=false, indent=2)
Else: # If the instructor information is already in the file
If Dic.get (newteachername): # does not exist in the dictionary key, then return none, no error
Print ("%s instructor already exists, cannot create lecturer with the same name"% 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} # Store instructor information in a dictionary
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 course already exists, cannot register course number same course"% 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 ("" "
-------------Instructor Information-------------
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 ("" "
---------------Student Information--------------
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 (): # Student View
While True:
Pro = "" "
1. Welcome to Register
2. Return
3. Exit

Please select:
"""
num = input (PRO)
if num = = ' 1 ':
Choice_school_obj. Recruitstu () # Call Student Registration method and generate student object
elif num = = ' 2 ':
Break
elif num = = ' 3 ':
Sys.exit ()
Else
Continue


Def teacher_view (): # Instructor View
Name = input ("Please enter the instructor name:")
While True:
If Dic.get (name) or Teachers_dict.get (name):
Print ("Welcome to%s lecturer". Center (+, '-')% name)
Elif not Dic.get (name) and not Teachers_dict.get (name):
Print ("%s lecturer does not exist"% name)
Break
Pro = "" "
1. View student Information
2. Return
3. Exit

Please enter your choice:
"""
num = input (PRO)
if num = = ' 1 ':
If Teachers_dict.get (name):
Teachers_dict[name].show_student () # View student Information
Else
Print ("Not fully functional, only input Lvah,cheng")
elif num = = ' 2 ':
Break
elif num = = ' 3 ':
Sys.exit ()
Else
Continue


Def school_view (): # School View
While True:
Pro = "" "
1. Create a Course
2. Enrolment Registration
3. Hiring Lecturers
4. Return
5. Exit

Please enter your choice:
"""
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 # Variables
Global Choice_school_obj
DIC = {}

While True:

Prola = "" "
1. Login
2. Registration
Please select your required serial number:
"""

Choise = input (Prola)
if choise = = ' 1 ':
Login ()
elif choise = = ' 2 ':
Regiter ()
Print ("Please select School". Center (50, ' * '))
Pro1 = "" "
1.%s
2.%s
3. Return
4. Exit

Please enter your choice:
"" "% (school1. Schoolname, School2. Schoolname)
Choice_school = input (Pro1)
if Choice_school = = ' 1 ':
Choice_school_obj = school1 # Passing an object reference to 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 (select View. Center (50, ' * '))
Pro2 = "" "
1. Student View
2. Instructor View
3. Administrator View
4. Return
5. Exit

Please select a view:
"""

num = input (PRO2)

if num = = ' 1 ':
If status_dict[' id '] = = ' Student ': #做登录账号判断
Print ("Welcome to Learner View". Center (50, ' * '))
Students_view ()
Else
Print ("The user you are logged in to is not correct, please sign in again ~")
Login ()
elif num = = ' 2 ':
If status_dict[' id '] = = ' Teacher ':
Print ("Welcome to Instructor View". Center (50, ' * '))
Teacher_view ()
Else
Print ("The user you are logged in to is not correct, please sign in again ~")
Login ()
elif num = = ' 3 ':
If status_dict[' id '] = = ' manager ':
Print ("Welcome to school Administration View". Center (50, ' * '))
School_view ()
Else
Print ("The user you are logged in to is not correct, please sign in again ~")
Login ()
elif num = = ' 4 ':
Break
elif num = = ' 5 ':
Sys.exit ()
Else
Continue


if __name__ = = ' __main__ ':
Teachers_dict = {}
Courses_dict = {}
Stus_dict = {}

School1 = School ("sh old boy", "Shanghai Campus", "Motherland, Honor, responsibility") # Instantiation of two schools
School2 = School ("BJ old Boy", "Beijing Campus", "patriotic, Realistic, endeavor")

T1 = Teacher ("Alex", "page", "Male", "01", "10000")
T2 = Teacher ("Taibai", "X", "female", "02", "9000") # Instantiate two lecturers
teachers_dict["Alex"] = T1
teachers_dict["Taibai"] = T2
Teacher_dict = {"TeacherName": "Alex", "Teacherage": "", "" Teachersex ":" Male "," Courseno ":" 10000 "," Salary "
Teacher_dict = {"TeacherName": "Taibai", "teacherage": "+", "Teachersex": "Female", "Courseno": "02",
"Salary": "9000"}

Course1 = Course ("Down", "Linux Cloud Automation Operations", "12800") # Instantiate two courses
Course2 = Course ("Up", "Python", "9800")
Courses_dict["[]] = Course1
courses_dict["Course2" =
Courses_dict = {"Courseno": "Down", "Coursename": "Linux Cloud Automation Operation", "Courseprice": "12800"}
Courses_dict = {"Courseno": "Up", "coursename": "Python", "Courseprice": "9800"}

STU1 = Stu ("Katy", "All", "female", "Alex", "" "" 3150911026 "," 12800 ") # #实例化两个学员
STU2 = Stu ("Betty", "+", "male", "Taibai", "02", "3150911022", "12000")
stus_dict["Katy"] = STU1
stus_dict["Betty"] = STU2
Stu_dict = {"Stuname": "Static Fragrance", "Stuage": "", "" Stusex ":" Female "," Stuschool ":" Sh old boy "," Courseno ":" 01 ",
"Stuid": "3150911026", "Courseprice": "12800"}
Stu_dict = {"Stuname": "Nobita", "Stuage": "All", "stusex": "Male", "Stuschool": "BJ Old boy", "Courseno": "02",
"Stuid": "3150911022", "Courseprice": "12000"}
Print (School1, school2)
Main ()

Python Learning Day7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.