python student table

import json
import getpass

def menu():
"""
主功能界面
:return:
"""
print("*"*53)
print('*' + ' '*51 + '*')
print('*' + 'Student Manager System'.center(51,' ') + '*')
print('*' + '1.Log in'.center(51,' ') + '*')
print('*' + '2.Register'.center(51,' ') + '*')
print('*' + '0.Exit '.center(51,' ') + '*')
print('*' + ' ' * 51 + '*')
print("*" * 53)
while True:
option = input('输入选项:')
if option == '1':
login()
elif option == '2':
register()
elif option == '0':
exit()
the Option == elif '':
the Continue
the else:
Print ( 'input is incorrect, please re-select items!')

DEF the Login ():
"" "
user login
: return:
" ""
Print ( '*' + 'Starting the Login '.center (50,' ') +' * ')
F1 = Open (' ./ rmyfile / system_info.json ',' R & lt ', encoding =' UTF-. 8 ')
RES = f1.read ()
f1.close ()
sys_info_list = json.loads (RES)
the while True:
t_id = iNPUT ( 'enter ID:')
t_pwd = getpass.getpass ( 'password:')
for n-in Range (len (sys_info_list)):
IF sys_info_list [n- ] [ 'id'] == t_id and sys_info_list [n-] [ 'pwd'] == t_pwd:
submenu (sys_info_list [n-]) ## returns the user dictionary information
the else:
Print ( 'no such user or user password does not match, please log in again')



DEF the Register ():
"" "
User Registration
: return:
" ""
Print ( '*' + 'Starting register'.center (50, '') + '*')
F1 = Open ( './ rmyfile / system_info.json', 'R & lt', encoding = 'UTF-. 8')
RES = f1.read ()
f1.close ()
sys_info_list = JSON. loads (RES)
the while True:
t_id = iNPUT ( 'enter user id:')
for n-in Range (len (sys_info_list)):
IF t_id == sys_info_list [n-] [ 'id']:
Print ( 'has the user id present ').
BREAK
elif t_id ==' ':
print ( 'user id can not be empty.')
BREAK
the else:
BREAK
t_user the INPUT = ( 'Enter a user name:')
the while True:
t_pwd = getpass.getpass ( 'password:')
t_pwd2 = getpass.getpass ( 'Confirm Password:')
! = IF t_pwd t_pwd2:
Print ( ' password inconsistent ')
the else:
BREAK

new_info_dict = {}
new_info_dict [' ID '] = t_id
new_info_dict [' User '] = T_USER
new_info_dict [' pwd '] = t_pwd
new_info_dict [' Students. '] = []
sys_info_list.append (new_info_dict)
sys_info_json = json.dumps (sys_info_list) # added to complete the conversion list information format json
f2 = open ( './ rmyfile / system_info.json', 'w', encoding = 'utf-8')
f2.write(sys_info_json)
print ( 'User:% s'% t_id + 'successful registration')
f2.close ()

print ( 'options: continue to register 1 / return a 0')
the while True:
Option = INPUT ( 'Please input options:' )
IF Option == '. 1':
Register ()
elif Option == '0':
MENU ()
elif Option == '':
Continue
the else:
Print ( '. entered incorrectly, please re-select')


DEF submenu (teacher_dict : dict):
"" "
subfunction interface - the user logs
: param teacher_dict:
: return:
" ""
Print ( "*" * 53 is)
Print ( '*' + ( 'available for purchase to you -% S'% teacher_dict [ 'user']). center ( 50,' ') + '*')
print('*' + '1.Add student'.center(50, ' ') + '*')
print('*' + '2.Edit student'.center(50, ' ') + '*')
print('*' + '3.Query student'.center(50, ' ') + '*')
print('*' + '4.Delete student'.center(50, ' ') + '*')
print('*' + '5.Return menu'.center(50, ' ') + '*')
print('*' + '0.Exit'.center(50, ' ') + '*')
print("*" * 53)
while True:
option = input('输入选项:')
if option == '1':
add_student(teacher_dict)
elif option == '2':
edit_student(teacher_dict)
elif option == '3':
query_student(teacher_dict)
elif option == '4':
delete_student(teacher_dict)
elif option == '5':
the MENU ()
elif the Option == '0':
Exit ()
elif the Option == '':
the Continue
the else:
Print ( 'input is incorrect, please re-select items!')

DEF add_student (teacher_dict: dict):
Print ( ' * '+' Adding student'.center (50, '') + '*')
F1 = Open ( './ rmyfile / system_info.json', 'R & lt', encoding = 'UTF-. 8')
RES = F1. Read ()
f1.close ()
sys_info_list = json.loads (RES) # convert text into a format python json format, here List
stu_name = iNPUT ( 'enter a student name:')
the while True:
stu_id = iNPUT ( 'enter the student number:')
IF stu_id == '':
Continue
for n-in Range (len (teacher_dict [ 'Students.'])): # student number must not be the same
== teacher_dict stu_id IF [ 'Students.'] [n-] [ 'stuid']:
Print ( '. student number is occupied, reset')
break
the else: # Note that if the above cycle ends break, is not performed else the
BREAK

Age = iNPUT ( 'input Age:')
phone = iNPUT ( 'enter phone number:')
student_dcit = { 'stuname': stu_name, 'stuid': stu_id, 'Age': Age, 'phone': phone } # generates new student dictionary
t_index = sys_info_list.index (teacher_dict) # teacher system dictionary information in the index information list
sys_info_list [t_index] [ 'students' ]. append (student_dcit) # students to add new system information list
sys_info_json = json .dumps (sys_info_list)
F2 = Open ( './ rmyfile / system_info.json', 'W', encoding = 'UTF-. 8')
f2.write (sys_info_json)
F2.use Close ()
Print ( 'add students (% s) successfully. \ n'% stu_name)
print ( 'options: continue to add 1 / return a 0')
the while True:
Option = INPUT ( 'Please input options:')
IF Option == '1':
add_student (sys_info_list [t_index])
elif Option == ' 0 ':
submenu (sys_info_list [t_index])
elif Option ==' ':
Continue
the else:
Print (' input is incorrect, please re-select \ n-. ')
DEF edit_student (teacher_dict: dict):
Print (' * '+' student'.center Editing (50, '') + '*')
print ( 'you all student information as follows:')
for the n-in the Range (len (teacher_dict [ 'students'])): # print all students to
print ( teacher_dict [ 'Students.'] [n-])

F1 = Open ( './rmyfile/system_info.json', 'r', encoding='utf-8')
res = f1.read()
f1.close ()
sys_info_list = json.loads (RES) # convert text python json format to format, here List
t_index = sys_info_list.index (teacher_dict) # teachers dictionary information system information in the index list
while True :
edit_id the iNPUT = ( 'enter need to modify student id:') # first do not enter the student id case
Mark = 0
IF edit_id == '':
the Continue
for the n-in the Range (len (teacher_dict [ 'students' ])):
IF teacher_dict [ 'students'] [the n-] [ 'stuid'] == edit_id: # find matching students
edit_name = input ( 'change the name:')
edit_age the INPUT = ( 'modify Age:')
edit_phone = input ( 'modify a phone number:')
teacher_dict [ 'Students.'] [n-] [ 'stuname'] = edit_name
teacher_dict [ 'Students.'] [n-] [ 'Age'] = edit_age
teacher_dict [ 'Students.'] [n-] [ 'Phone'] = edit_phone
sys_info_list [t_index] [ 'Students.'] [n-] = teacher_dict [ 'Students.' ] [n] .copy () # modify the dictionary corresponding student
Mark. 1 =
sys_info_json = json.dumps (sys_info_list)
F2 = Open ( './ rmyfile / system_info.json', 'W', encoding = 'UTF-. 8 ')
f2.write (sys_info_json)
f2.close ()
Print (' modify student information successfully \ the n-. ')
BREAK
IF not Mark:
Print (' students to be modified does not exist (% s) \ n '% edit_id).
BREAK
Print ( 'Options: 1 continue to modify / return to the previous 0 ')
the while True:
option = input ( 'Enter option:')
IF Option == '. 1':
edit_student (sys_info_list [t_index])
elif Option == '0':
submenu (sys_info_list [t_index])
elif Option == '':
Continue
the else :
Print ( 'input is incorrect, please re-select.')

DEF delete_student (teacher_dict: dict):
Print ( '*' + 'the Deleting student'.center (50,' ') +' * ')
Print (' your All students information is as follows: ')
for n-in Range (len (teacher_dict [' students. '])): # print all students
Print (teacher_dict [' students. '] [n-])
F1 = Open (' ./ rmyfile / system_info. json ',' r ', encoding =' utf-8 ')
res = f1.read()
f1.close()
sys_info_list = json.loads (res) # convert text python json format to format, here List
t_index = sys_info_list.index (teacher_dict) # teachers dictionary information system information in the index list of
the while True:
del_id the INPUT = ( 'enter the need to remove the student id:') # first do not enter the student id case
Mark = 0
IF del_id == '':
the Continue
for the n-in the Range (len (teacher_dict [ 'students'])):
IF teacher_dict [ 'students'] [n ] [ 'stuid'] == del_id: # find a matching student ID
. sys_info_list [t_index] [ 'students.'] POP (n-) # delete the corresponding student dictionary
sys_info_json = json.dumps ( sys_info_list)
F2 = Open ( './ rmyfile / system_info.json', 'W', encoding = 'UTF-. 8')
F2.write(sys_info_json)
f2.close()
print ( 'remove students (% s) successfully \ the n-.'% del_id)
Mark = 1
BREAK
IF not Mark: # Without this student to prompt
print ( 'no id (% s) students \ n'% del_id )
BREAK
Print ( 'options: continue to remove 1 / return to previous 0')
the while True:
the option = the iNPUT ( 'Please input options:')
IF the option == '1':
delete_student (sys_info_list [t_index])
elif the option = = '0':
submenu (sys_info_list [t_index])
elif Option == '':
Continue
the else:
Print ( 'input is incorrect, please re-selection.')


DEF query_student (teacher_dict: dict):
Print ( '*' + 'Querying student'.center(51, ' ') + '*')
Print ( '*' + '' + 51 is * '*')
Print ( '*' + '1.Query by Student name'.center (51 is,' ') +' * ')
Print (' * '+' 2 by Student id'.center .query (51 is, '') + '*')
Print ( '*' + '3.Query by age'.center (51 is,' ') +' * ')
the while True:
Option = input ( 'enter the options:')
IF the option == '1':
query_student_name (teacher_dict)
elif the option == '2':
query_student_id (teacher_dict)
elif the option == '':
the Continue
the else:
Print ( 'a mistake, Please re-select ').

DEF query_student_name (teacher_dict: dict):
Print ( '*' + 'Querying by name'.center (51,' ') +' * ')
(' Enter the student's name to be queried: ') query_name = input
COUNT = 0
for n-in Range (len (teacher_dict [ 'Students.'])):
IF QUERY_NAME in teacher_dict [ 'Students.'] [n-] [ 'stuname']:
COUNT + =. 1
Print (teacher_dict [ 'Students.'] [ the n-])
Print ( "% d in total for you to find records \ the n-'% COUNT)
Print (' option: to research 1 / return to previous 0 ')
the while True:
the option = the iNPUT (' Please input options: ')
Option == IF '. 1':
query_student (teacher_dict)
elif Option == '0':
submenu (teacher_dict)
elif Option == '':
Continue
the else:
Print ( '. entered incorrectly, please re-select')


DEF query_student_id ( teacher_dict: dict):
Print ( '*' + 'Querying by id'.center (51 is,' ') +' * ')
(' need, student number of inputs: ') = query_id INPUT
COUNT = 0
for n-in Range (len (teacher_dict [ 'Students'])):
IF query_id == teacher_dict [' Students'] [the n-] [ 'stuid']:
COUNT = 1 +
Print (teacher_dict [ 'Students'] [the n-])
Print (' in total for you to find records% d \ n-'COUNT%)
Print (' options: 1 to research / return a 0 ')
the while True:
option = iNPUT (' Please input options: ')
IF option ==' 1 ':
query_student ( teacher_dict)
elif Option == '0':
submenu (teacher_dict)
the else:
Print ( 'Entered incorrectly, please re-select ').

IF __name__ ==' __main__ ':
the MENU (

Guess you like

Origin www.cnblogs.com/cheyunhua/p/10930711.html