
接下来把剩下的实验一起写上去
实验2
写一个学生类,属性有学号,姓名,成绩(三门),方法有输出,求平均成绩。
设计思路:
1. 先写一个学生类,并向里面写一个求平均值和输出信息的方法。
2. 在写一个执行端口的函数,有登记学生信息,查看学生信息和退出的操作。
3. 选择查看时,会先打印出学生列表,再选择学生,会出现一个选择端口有查看学生信息和平均值的操作。
4. 最后通过各种语句与框架结合起来
我写的是一个类似学生管理系统的一小部分,
class Student(object): # 定义一个学生类
def __init__(self, stu_id, name, score1, score2, score3): # 实例化
self.stu_id = stu_id
self.name = name
self.score1 = score1
self.score2 = score2
self.score3 = score3
def average_(self):
'''求平均值,并打印出来'''
ave_score = (int(self.score1) + int(self.score2) + int(self.score3)) / 3
print('%s 学生语数英平均值:%s 分' % (self.name, ave_score))
def information(self):
'''打印个人信息'''
print('''
-----Student %s-------
学号:%s
语文:%s 分
数学:%s 分
英语:%s 分
''' % (self.name, self.stu_id, self.score1, self.score2, self.score3))
def judge_digit(n):
'''判断 输入的num 是不是 数字字符 若是则返回成整数 若不是则重新输入'''
while True:
num = input('%s成绩:' % n)
if num.isdigit():
num = int(num)
return num
else:
print('please input number')
def register(stu_list):
'''学生登记函数'''
while True:
print('''
-----Student Register-------
【输入T登记,输入F返回】
''')
temp = input('命令:')
if temp == 'T':
name = input('学生名字:')
stu_id = input('学号:')
score1 = judge_digit('语文')
score2 = judge_digit('数学')
score3 = judge_digit('英语')
stu = Student(stu_id, name, score1, score2, score3)
stu_list.append(stu) # 向学生列表添加该学生的地址
print('登记成功!')
elif temp == 'F':
break
else:
print('I don not know what you want to do.\tPlease input again!')
return stu_list
def inqurie(stu_list):
'''查看学生相关档案函数'''
judge = True
while judge:
if len(stu_list) == 0: # 当学生列表没有学生时,打印无学生并返回
print('暂无学生')
break
else: # 否则打印学生列表
for index, i in enumerate(stu_list):
print(index + 1, i.name)
tem = input('输入F 返回,输入T继续') if tem == 'F':
judge = False elif tem == 'T':
stu_num = input('查看的学生:') if stu_num.isdigit():
stu_num = int(stu_num)
if len(stu_list) >= stu_num: # 当输入的序号有对应的学生
while True:
print('''
-------Student %s--------
1.学号及成绩
2.平均分
3.返回
''' % stu_list[stu_num - 1].name)
stu_handle = input('操作:')
if stu_handle.isdigit():
stu_handle = int(stu_handle) if stu_handle == 1: # 执行学生类里输出方法
stu_list[stu_num - 1].information() elif stu_handle == 2: # 执行类里平均值方法
stu_list[stu_num - 1].average_() elif stu_handle == 3:
break else:
print('please input again!')
else:
print('please input again!')
else:
print('please input again!')
else:
print('please input again!')
else:
print('please input again!')
def main():
'''实行窗口'''
stu_list = []
# s1=Student('001','陈浩南',100,100,100)#可以在此处添加初始学生
# stu_list.append(s1)
while True:
print('''
------学生档案------
1.登记
2.查询
3.退出程序
''')
temp = input('操作(输入编号):')
if temp.isdigit():
temp = int(temp)
if temp == 1: # 执行学生登记的函数
stu_list = register(stu_list)
elif temp == 2: # 执行查看学生档案的函数
inqurie(stu_list)
elif temp == 3:
break
else:
print('please input again!')
else:
print('please input again!')
main()
实验3
将学生信息(上次上机的学生类)存入文本文件,已及从文本文件读入信息输出学生信息
设计思路:
1. 先写把上次的学生类复制过来并修改
2. 创建一个存储‘学生信息’的文本
3. 在学生类里写一个把类的属性写到文本里的方法
4. 在写个读并打印文本里的信息的函数
5. 最后通过各种语句与框架结合起来
class Student(object): # 定义一个学生类
def __init__(self, stu_id, name, score1, score2, score3): # 实例化
self.stu_id = stu_id
self.name = name
self.score1 = score1
self.score2 = score2
self.score3 = score3
def average_(self):
'''求平均值,并打印出来'''
ave_score = (int(self.score1) + int(self.score2) + int(self.score3)) / 3
print('%s 学生语数英平均值:%s 分' % (self.name, ave_score))
def canned_date(self):
'''把信息写入文本中 '''
with open('D:\\学生信息.txt','a',encoding='utf-8') as f1:
f1.write('\n')
f1.write(self.name.ljust(7))
f1.write(str(self.stu_id).ljust(8))
f1.write(str(self.score1).ljust(6))
f1.write(str(self.score2).ljust(6))
f1.write(str(self.score3).ljust(6))
print('>>>>>>>写入成功')
def judge_digit(n):
'''判断 输入的num 是不是 数字字符 若是则返回成整数 若不是则重新输入'''
while True:
num = input('%s成绩:'%n)
if num.isdigit():
num = int(num)
return num
else:
print('please input number')
def judge_digit(n):
'''判断 输入的num 是不是 数字字符 若是则返回成整数 若不是则重新输入'''
while True:
num = input('%s成绩:'%n)
if num.isdigit():
num = int(num)
return num
else:
print('please input number')
def information():
'''打印学生信息'''
with open('D:\\学生信息.txt', 'r', encoding='utf-8')as f:
for i in f:
print(i)
def build_Text():
'''创建学生信息文本,用以存储信息'''
with open('D:\\学生信息.txt','w',encoding='utf-8')as c:
c.write('姓名 学号 语文 数学 英语')
def main():
'''实行窗口'''
build_Text()#第一次才需使用,以后可将其注释掉
while True:
print('''
------学生档案------
1.登记
2.查询
3.退出程序
''')
temp = input('操作(输入编号):')
if temp.isdigit():
temp = int(temp)
if temp == 1: # 执行学生登记的函数
register()
elif temp == 2: # 实行打印学生信息的函数
information()
elif temp == 3:
break
else:
print('please input again!')
else:
print('please input again!')
main()
基础和经验有限- -