pythonに基づいて学生管理システムを実現する。
本論文では、pythonが学生管理システムを実現するための具体的なコードを共有しました。
1.0バージョン学生管理システム
1.0バージョン学生管理システム
'''
1.
2.
3.
4.
0.
'''
student_list=[]
while True:
print('1. ')
print('2. ')
print('3. ')
print('4. ')
print('0. ')
sel_num=input(' :')
sel_num=int(sel_num)
# 0~5
while sel_num not in range(0,5):
sel_num=input(' , :')
sel_num=int(sel_num)
if sel_num==1:
name=input(' :')
age=input(' :')
sex=input(' :')
person_list=[name,age,sex]
student_list.append(person_list)
print(' !')
elif sel_num==2:
for x in range(0,len(student_list)):
person=student_list[x]
print(' :%s :%s :%s :%s '%(x,person[0],person[1],person[2]))
index=input(' :')
index=int(index)
while index not in range(0, len(student_list)):
index = input(' , :')
index = int(index)
person=student_list[index]
name=person[0]
age=person[1]
sex=person[2]
student_list[index][0]=input(' :(%s):'%name)
student_list[index][1]=input(' :(%s):'%age)
student_list[index][2]=input(' :(%s)'%sex)
print(' !')
elif sel_num==3:
for x in range(0,len(student_list)):
person=student_list[x]
name=person[0]
age=person[1]
sex=person[2]
print(' :%s :%s :%s :%s '%(x,name,age,sex))
elif sel_num==4:
for x in range(0,len(student_list)):
person=student_list[x]
print(' :%s :%s :%s :%s '%(x,person[0],person[1],person[2]))
print('1. ')
print('2. ')
num=input(' :')
if num=='1':
student_list.clear()
else:
index = input(' :')
index = int(index)
while index not in range(0, len(student_list)):
index = input(' , :')
index = int(index)
del student_list[index]
else:
break
2.0バージョンの学生管理システム---関数版--学生情報をリストで保存する
#
def add_student():
# 、 、
name=input(' :')
age=input(' :')
phone=input(' :')
# name、age、phone
student=[name,age,phone]
#
# append(object) insert(index,object) extend(iterable)
student_list.append(student)
print(' !')
#
def query_student():
#1.
#2.
print('1. ')
print('2. ')
num=int(input(' :'))
while num not in range(1,3):
num=int(input(' , :'))
if num==1:
print('************** ***************')
#
for x in range(0,len(student_list)):
# x
student=student_list[x]
# 、 、
name=student[0]
age=student[1]
phone=student[2]
print(' :%s :%s :%s :%s'%(x,name,age,phone))
else:
name = input(' :')
while 1:
a=False
for student in student_list:
if student[0] == name:
index = student_list.index(student, 0, 8)
print(' :%s :%s :%s :%s'%(index,student_list[index][0],student_list[index][1],student_list[
index][2]))
a=True
if a==False:
name=input(' , :')
else:
break
#
def update_student():
# , ,
if len(student_list)==0:
print(' , !')
# return
return
#1.
query_student()
#2.
num=input(' :')
#3.
num=int(num)
#4.
while num not in range(0,len(student_list)):
# ,
num=input(' , :')
num=int(num)
#5.
student=student_list[num]
new_name=input(' (%s):'%student[0])
new_age=input(' (%s)'%student[1])
new_phone=input(' (%s)'%student[2])
#6.
student[0]=new_name
student[1]=new_age
student[2]=new_phone
print(' !')
#
#1. 2. 3. ( )
def delete_student():
if len(student_list)==0:
print(' , !')
return
print('1. ')
print('2. ')
print('3. ')
#
num=int(input(' :'))
#
while num not in range(1,4):
num=int(input(' , '))
#
if num == 1:
# 1.
query_student()
#2.
num=int(input(' :'))
#
while num not in range(0,len(student_list)):
num=int(input(' , !'))
is_del=input(' (%s) ?(y/n):'%student_list[num][0])
if is_del=='y':
#
del student_list[num]
#student_list.pop(index)
print('%s !'%student_list[num][0])
if num==2:
#
is_del=input(' ?y( )/n( ):')
if is_del=='y':
#
student_list.clear()
print(' !')
else:
print(' !')
else:
name = input(' :')
while 1:
# name
list = []
#
for student in student_list:
# name name
if student[0] != name:
# name
index = student_list.index(student,0,len(student_list))
# list
list.append(student_list[index])
# name
if len(student_list) == len(list):
name = input(' , :')
#
else:
#
student_list.clear()
# list
for dict in list:
student_list.append(dict)
break
#1. ,
student_list=[]
#2.while
while True:
print('*********** V2.0**************')
print('1. ')
print('2. ')
print('3. ')
print('4. ')
print('0. ')
print('***************************************')
#
num=input(' :')
#
num=int(num)
#
while num not in range(0,5):
#
num=input(' , :')
#
num=int(num)
#
if num==1:
#
add_student()
elif num==2:
#
query_student()
elif num==3:
#
update_student()
elif num==4:
delete_student()
else:
print(' !')
break
2.1バージョン学生管理システム---関数版--辞書で学生情報を保存する
#
def add_student():
# 、 、
name=input(' :')
age=input(' :')
phone=input(' :')
# name、age、phone
student={'name':name,'age':age,'phone':phone}
#
# append(object) insert(index,object) extend(iterable)
student_list.append(student)
print(' !')
#
def query_student():
#1.
#2.
print('1. ')
print('2. ')
num=int(input(' :'))
while num not in range(1,3):
num=int(input(' , :'))
if num==1:
print('************** ***************')
#
for x in range(0,len(student_list)):
# x
student=student_list[x]
# 、 、
name=student['name']
age=student['age']
phone=student['phone']
print(' :%s :%s :%s :%s'%(x,name,age,phone))
else:
name = input(' :')
while 1:
a=False
for student in student_list:
if student['name'] == name:
index = student_list.index(student, 0, 8)
print(' :%s :%s :%s :%s'%(index,student_list[index]['name'],student_list[index]['age'],
student_list[index]['phone']))
a=True
if a==False:
name=input(' , :')
else:
break
#
def update_student():
# , ,
if len(student_list)==0:
print(' , !')
# return
return
#1.
query_student()
#2.
num=input(' :')
#3.
num=int(num)
#4.
while num not in range(0,len(student_list)):
# ,
num=input(' , :')
num=int(num)
#5.
student=student_list[num]
new_name=input(' (%s):'%student['name'])
new_age=input(' (%s)'%student['age'])
new_phone=input(' (%s)'%student['phone'])
#6.
student['name']=new_name
student['age']=new_age
student['phone']=new_phone
print(' !')
#
#1. 2. 3. ( )
def delete_student():
if len(student_list)==0:
print(' , !')
return
print('1. ')
print('2. ')
print('3. ')
#
num=int(input(' :'))
#
while num not in range(1,4):
num=int(input(' , '))
#
if num == 1:
# 1.
query_student()
#2.
num=int(input(' :'))
#
while num not in range(0,len(student_list)):
num=int(input(' , !'))
is_del=input(' (%s) ?(y/n):'%student_list[num]['name'])
if is_del=='y':
#
del student_list[num]
#student_list.pop(index)
print('%s !'%student_list[num]['name'])
else:
print(' !')
elif num==2:
#
is_del=input(' ?y( )/n( ):')
if is_del=='y':
#
student_list.clear()
print(' !')
else:
print(' !')
else:
name = input(' :')
while 1:
# name
list=[]
#
for student in student_list:
# name name
if student['name']!=name:
# name
index=student_list.index(student)
# list
list.append(student_list[index])
# name
if len(student_list)==len(list):
name=input(' , :')
#
else:
#
student_list.clear()
# list
for dict in list:
student_list.append(dict)
break
#1. ,
student_list=[]
#2.while
while True:
print('*********** V2.0**************')
print('1. ')
print('2. ')
print('3. ')
print('4. ')
print('0. ')
print('***************************************')
#
num=input(' :')
#
num=int(num)
#
while num not in range(0,5):
#
num=input(' , :')
#
num=int(num)
#
if num==1:
#
add_student()
elif num==2:
#
query_student()
elif num==3:
#
update_student()
elif num==4:
delete_student()
else:
print(' !')
break
3.0バージョン学生管理システム--学生情報をファイルに保存する
def is_in_range():
index = input(' ( ) :')
index = int(index)
while index not in range(0, len(student_list)):
index = input(' , :')
index = int(index)
return index
def add_stu():
name = input(' :')
age = input(' :')
sex = input(' :')
person_list = [name, age, sex]
student_list.append(person_list)
print(' !')
def alter_stu():
index=is_in_range()
person = student_list[index]
name = person[0]
age = person[1]
sex = person[2]
student_list[index][0] = input(' :(%s):' % name)
student_list[index][1] = input(' :(%s):' % age)
student_list[index][2] = input(' :(%s)' % sex)
print(' !')
def see_stu():
for x in range(0, len(student_list)):
person = student_list[x]
name = person[0]
age = person[1]
sex = person[2]
print(' :%s :%s :%s :%s ' % (x, name, age, sex))
def del_stu():
print('1. ')
print('2. ')
num = input(' :')
if num == '1':
student_list.clear()
else:
index = is_in_range()
del student_list[index]
#
def save_data():
#1.
file_handle=open('student_v2.txt',mode='w')
#2.for
for student in student_list:
#
s=' '.join(student)
#
file_handle.write(s)
file_handle.write('
')
#3.
file_handle.close()
# os
import os
#
def read_data():
#
rs=os.path.exists('student_v2.txt')
if rs==True:
#1.
file_handle=open('student_v2.txt',mode='r')
#2.
contents=file_handle.readlines()
for content in contents:
# /n
content=content.strip('
')
# ,
list_1=content.split(' ')
#
student_list.append(list_1)
# 3.
file_handle.close()
# ,
student_list = []
read_data()
while True:
print('1. ')
print('2. ')
print('3. ')
print('4. ')
print('0. ')
sel_num = input(' :')
sel_num = int(sel_num)
# 0~5
while sel_num not in range(0, 5):
sel_num = input(' , :')
sel_num = int(sel_num)
#
if sel_num == 1:
add_stu()
save_data()
elif sel_num == 2:
# 1.
see_stu()
# 2.
alter_stu()
save_data()
elif sel_num == 3:
see_stu()
elif sel_num == 4:
see_stu()
# 2.
del_stu()
save_data()
else:
break
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。