Python 学习之路3

接下来把剩下的实验一起写上去

实验2

  写一个学生类,属性有学号,姓名,成绩(三门),方法有输出,求平均成绩。

设计思路:

1.         先写一个学生类,并向里面写一个求平均值和输出信息的方法。

2.         在写一个执行端口的函数,有登记学生信息,查看学生信息和退出的操作。

3.         选择查看时,会先打印出学生列表,再选择学生,会出现一个选择端口有查看学生信息和平均值的操作。

4.         最后通过各种语句与框架结合起来

我写的是一个类似学生管理系统的一小部分,

 1 class Student(object):  # 定义一个学生类
 2     def __init__(self, stu_id, name, score1, score2, score3):  # 实例化
 3         self.stu_id = stu_id
 4         self.name = name
 5         self.score1 = score1
 6         self.score2 = score2
 7         self.score3 = score3
 8     def average_(self):
 9         '''求平均值,并打印出来'''
10         ave_score = (int(self.score1) + int(self.score2) + int(self.score3)) / 3
11         print('%s 学生语数英平均值:%s 分' % (self.name, ave_score))
12     def information(self):
13         '''打印个人信息'''
14         print('''
15 -----Student %s-------
16 学号:%s
17 语文:%s 分
18 数学:%s 分
19 英语:%s 分
20                 ''' % (self.name, self.stu_id, self.score1, self.score2, self.score3))
View Code
1 def judge_digit(n):
2     '''判断 输入的num 是不是 数字字符 若是则返回成整数 若不是则重新输入'''
3     while True:
4         num = input('%s成绩:' % n)
5         if num.isdigit():
6             num = int(num)
7             return num
8         else:
9             print('please input number')
View Code
 1 def register(stu_list):
 2     '''学生登记函数'''
 3     while True:
 4         print('''
 5 -----Student Register-------
 6     【输入T登记,输入F返回】
 7     ''')
 8         temp = input('命令:')
 9         if temp == 'T':
10             name = input('学生名字:')
11             stu_id = input('学号:')
12             score1 = judge_digit('语文')
13             score2 = judge_digit('数学')
14             score3 = judge_digit('英语')
15             stu = Student(stu_id, name, score1, score2, score3)
16             stu_list.append(stu)  # 向学生列表添加该学生的地址
17             print('登记成功!')
18         elif temp == 'F':
19             break
20         else:
21             print('I don not know what you want to do.\tPlease input again!')
22     return stu_list
View Code
 1 def inqurie(stu_list):
 2     '''查看学生相关档案函数'''
 3     judge = True
 4     while judge:
 5         if len(stu_list) == 0:  # 当学生列表没有学生时,打印无学生并返回
 6             print('暂无学生')
 7             break
 8         else:  # 否则打印学生列表
 9             for index, i in enumerate(stu_list):
10                 print(index + 1, i.name)
11             tem = input('输入F 返回,输入T继续')
12 
13             if tem == 'F':
14                 judge = False
15 
16             elif tem == 'T':
17                 stu_num = input('查看的学生:')
18 
19                 if stu_num.isdigit():
20                     stu_num = int(stu_num)
21                     if len(stu_list) >= stu_num:  # 当输入的序号有对应的学生
22                         while True:
23                             print('''
24 -------Student %s--------
25 1.学号及成绩
26 2.平均分
27 3.返回
28                                 ''' % stu_list[stu_num - 1].name)
29                             stu_handle = input('操作:')
30                             if stu_handle.isdigit():
31                                 stu_handle = int(stu_handle)
32 
33                                 if stu_handle == 1:  # 执行学生类里输出方法
34                                     stu_list[stu_num - 1].information()
35 
36                                 elif stu_handle == 2:  # 执行类里平均值方法
37                                     stu_list[stu_num - 1].average_()
38 
39                                 elif stu_handle == 3:
40                                     break
41 
42                                 else:
43                                     print('please input again!')
44                             else:
45                                 print('please input again!')
46                     else:
47                         print('please input again!')
48                 else:
49                     print('please input again!')
50             else:
51                 print('please input again!')
View Code
 1 def main():
 2     '''实行窗口'''
 3     stu_list = []
 4     # s1=Student('001','陈浩南',100,100,100)#可以在此处添加初始学生
 5     # stu_list.append(s1)
 6     while True:
 7         print('''
 8 ------学生档案------
 9 1.登记
10 2.查询
11 3.退出程序
12         ''')
13         temp = input('操作(输入编号):')
14         if temp.isdigit():
15             temp = int(temp)
16             if temp == 1:  # 执行学生登记的函数
17                 stu_list = register(stu_list)
18             elif temp == 2:  # 执行查看学生档案的函数
19                 inqurie(stu_list)
20             elif temp == 3:
21                 break
22             else:
23                 print('please input again!')
24         else:
25             print('please input again!')
View Code
1 main()
View Code

 实验3   

    将学生信息(上次上机的学生类)存入文本文件,已及从文本文件读入信息输出学生信息

设计思路:

1.    先写把上次的学生类复制过来并修改

2.    创建一个存储‘学生信息’的文本

3.    在学生类里写一个把类的属性写到文本里的方法

4.    在写个读并打印文本里的信息的函数

5.    最后通过各种语句与框架结合起来

 1 class Student(object):  # 定义一个学生类
 2     def __init__(self, stu_id, name, score1, score2, score3):  # 实例化
 3         self.stu_id = stu_id
 4         self.name = name
 5         self.score1 = score1
 6         self.score2 = score2
 7         self.score3 = score3
 8     def average_(self):
 9         '''求平均值,并打印出来'''
10         ave_score = (int(self.score1) + int(self.score2) + int(self.score3)) / 3
11         print('%s 学生语数英平均值:%s 分' % (self.name, ave_score))
12     def canned_date(self):
13         '''把信息写入文本中 '''
14         with open('D:\\学生信息.txt','a',encoding='utf-8') as f1:
15             f1.write('\n')
16             f1.write(self.name.ljust(7))
17             f1.write(str(self.stu_id).ljust(8))
18             f1.write(str(self.score1).ljust(6))
19             f1.write(str(self.score2).ljust(6))
20             f1.write(str(self.score3).ljust(6))
21             print('>>>>>>>写入成功')
View Code
1 def judge_digit(n):
2     '''判断 输入的num 是不是 数字字符 若是则返回成整数 若不是则重新输入'''
3     while True:
4         num = input('%s成绩:'%n)
5         if num.isdigit():
6             num = int(num)
7             return num
8         else:
9             print('please input number')
View Code
1 def judge_digit(n):
2     '''判断 输入的num 是不是 数字字符 若是则返回成整数 若不是则重新输入'''
3     while True:
4         num = input('%s成绩:'%n)
5         if num.isdigit():
6             num = int(num)
7             return num
8         else:
9             print('please input number')
View Code
1 def information():
2     '''打印学生信息'''
3     with open('D:\\学生信息.txt', 'r', encoding='utf-8')as f:
4         for i in f:
5             print(i)
6 def build_Text():
7     '''创建学生信息文本,用以存储信息'''
8     with open('D:\\学生信息.txt','w',encoding='utf-8')as c:
9         c.write('姓名     学号     语文   数学   英语')
View Code
 1 def main():
 2     '''实行窗口'''
 3     build_Text()#第一次才需使用,以后可将其注释掉
 4     while True:
 5         print('''
 6 ------学生档案------
 7 1.登记
 8 2.查询
 9 3.退出程序
10         ''')
11         temp = input('操作(输入编号):')
12         if temp.isdigit():
13             temp = int(temp)
14             if temp == 1:  # 执行学生登记的函数
15                 register()
16             elif temp == 2:  # 实行打印学生信息的函数
17                 information()
18             elif temp == 3:
19                 break
20             else:
21                 print('please input again!')
22         else:
23             print('please input again!')
24 main()
View Code

基础和经验有限- -

posted @ 2017-11-27 11:41  实力至上村长  阅读(368)  评论(0编辑  收藏  举报