python基础小练习
def main(): number = int(input("请输入学生的总人数:")) # 输入要录入的学生总数 count = number # 用一个变量来保存这个学生总数 students = list() # 创建一个列表来存储学生信息 while(number): # 进入循环 student = {} # 学生信息列表 name = input("请输入学生的姓名:") # 输入学生姓名 student['name'] = name # 字典赋值 gender = input("请输入他的性别:") # 输入性别 student['gender'] = gender # 字典赋值 students.append(student) # 将这个信息新增到列表 number -= 1 # 学生总数-1 print("%d 个学生信息是==========> %s"%(count, students)) # 循环结束,打印列表信息 if __name__ == '__main__': main()
# 学生信息录入展示代码
# 输入要录入的学生总数
# 录入学生基本信息
# 学生信息录入完毕展示给用户
1 import os 2 import re 3 import numpy as np 4 5 class Student: #定义一个学生类 6 def __init__(self): 7 self.name = '' 8 self.ID ='' 9 self.score1 = 0 10 self.score2 = 0 11 self.score1 = 0 12 self.sum = 0 13 14 15 def searchByID(stulist, ID): #按学号查找看是否学号已经存在 16 for item in stulist: 17 if item.ID == ID: 18 return True 19 20 def Add(stulist,stu): #添加一个学生信息 21 if searchByID(stulist, stu.ID) == True: 22 print("学号已经存在!") 23 return False 24 stulist.append(stu) 25 print( stu.name,stu.ID, stu.score1, stu.score2, stu.score3, stu.sum) 26 print( "是否要保存学生信息?") 27 nChoose = input("Choose Y/N") 28 if nChoose == 'Y' or nChoose == 'y': 29 file_object = open("students.txt", "a") 30 file_object.write(stu.ID) 31 file_object.write(" ") 32 file_object.write(stu.name) 33 file_object.write(" ") 34 file_object.write(str(stu.score1)) 35 file_object.write(" ") 36 file_object.write(str(stu.score2)) 37 file_object.write(" ") 38 file_object.write(str(stu.score3)) 39 file_object.write(" ") 40 file_object.write(str(stu.sum)) 41 file_object.write("\n") 42 file_object.close() 43 print( u"保存成功!") 44 45 def Search(stulist, ID): #搜索一个学生信息 46 print( "学号 姓名 语文 数学 英语 总分") 47 count = 0 48 for item in stulist: 49 if item.ID == ID: 50 print( item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum) 51 break 52 count = 0 53 if count == len(stulist): 54 print( "没有该学生学号!") 55 56 def Del(stulist, ID): #删除一个学生信息 57 count = 0 58 for item in stulist: 59 if item.ID == ID: 60 stulist.remove(item) 61 print( "删除成功!") 62 break 63 count +=1 64 # if count == len(stulist): 65 # print( "没有该学生学号!" 66 file_object = open("students.txt", "w") 67 for stu in stulist: 68 print( stu.ID, stu.name, stu.score1,stu.score2, stu.score3, stu.sum) 69 file_object.write(stu.ID) 70 file_object.write(" ") 71 file_object.write(stu.name) 72 file_object.write(" ") 73 file_object.write(str(stu.score1)) 74 file_object.write(" ") 75 file_object.write(str(stu.score2)) 76 file_object.write(" ") 77 file_object.write(str(stu.score3)) 78 file_object.write(" ") 79 file_object.write(str(stu.sum)) 80 file_object.write("\n") 81 file_object.close() 82 # print( "保存成功!" 83 file_object.close() 84 def Change(stulist, ID): 85 count = 0 86 for item in stulist: 87 if item.ID == ID: 88 stulist.remove(item) 89 file_object = open("students.txt", "w") 90 for stu in stulist: 91 #print( li.ID, li.name, li.score 92 file_object.write(stu.ID) 93 file_object.write(" ") 94 file_object.write(stu.name) 95 file_object.write(" ") 96 file_object.write(str(stu.score1)) 97 file_object.write(" ") 98 file_object.write(str(stu.score2)) 99 file_object.write(" ") 100 file_object.write(str(stu.score3)) 101 file_object.write(" ") 102 file_object.write(str(stu.sum)) 103 file_object.write("\n") 104 # print( "保存成功!" 105 file_object.close() 106 stu = Student() 107 stu.name = input("请输入学生的姓名") 108 while True: 109 stu.ID = input("请输入学生的ID") 110 p = re.compile('^[0-9]{3}$') 111 if p.match(stu.ID): 112 break 113 else: 114 print( "输入的有错误!") 115 while True: 116 stu.score1 = int(input("请输入学生语文成绩")) 117 if stu.score1 <= 100 and stu.score1 > 0 : 118 break 119 else: 120 print( "输入的学生成绩有错误!") 121 while True: 122 stu.score2 = int(input("请输入学生数学成绩")) 123 if stu.score2 <= 100 and stu.score2 > 0 : 124 break 125 else: 126 print( "输入的学生成绩有错误!") 127 while True: 128 stu.score3 = int(input("请输入学生英语成绩")) 129 if stu.score3 <= 100 and stu.score3 > 0 : 130 break 131 else: 132 print( "输入的学生成绩有错误!") 133 stu.sum = stu.score1 + stu.score2 + stu.score3 134 Add(stulist,stu) 135 def display(stulist): #显示所有学生信息 136 print( u"学号 姓名 语文 数学 英语 总分") 137 for item in stulist: 138 print( item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum) 139 140 def Sort(stulist): #按学生成绩排序 141 Stu = [] 142 sum_count = [] 143 for li in stulist: 144 temp = [] 145 temp.append(li.ID) 146 temp.append(li.name) 147 temp.append(int(li.score1)) 148 temp.append(int(li.score2)) 149 temp.append(int(li.score3)) 150 temp.append(int(li.sum)) 151 sum_count.append(int(li.sum)) 152 Stu.append(temp) 153 154 #print( sum_count 155 #print( Stu; 156 #print( stulist 157 insertSort(sum_count, stulist) 158 #print( stulist; 159 display(stulist) 160 161 def insertSort(a, stulist): 162 for i in range(len(a)-1): 163 #print( a,i 164 for j in range(i+1,len(a)): 165 if a[i]<a[j]: 166 temp = stulist[i] 167 stulist[i] = stulist[j] 168 stulist[j] = temp 169 #return a 170 171 def Init(stulist): #初始化函数 172 print( "初始化......") 173 file_object = open('students.txt', 'r') 174 for line in file_object: 175 stu = Student() 176 line = line.strip("\n") 177 s = line.split(" ") 178 stu.ID = s[0] 179 stu.name = s[1] 180 stu.score1 = s[2] 181 stu.score2 = s[3] 182 stu.score3 = s[4] 183 stu.sum = s[5] 184 stulist.append(stu) 185 file_object.close() 186 print( "初始化成功!") 187 main() 188 189 def main(): #主函数 该程序的入口函数 190 while True: 191 print( "*********************") 192 print( u"--------菜单---------") 193 print( u"增加学生信息--------1") 194 print( u"查找学生信息--------2") 195 print( u"删除学生信息--------3") 196 print( u"修改学生信息--------4") 197 print( u"所有学生信息--------5") 198 print( u"按照分数排序--------6") 199 print( u"退出程序------------0") 200 print( "*********************") 201 202 nChoose = input("请输入你的选择:") 203 if nChoose == "1": 204 stu = Student() 205 stu.name = input("请输入学生的姓名") 206 while True: 207 stu.ID = input("请输入学生的ID") 208 p = re.compile('^[0-9]{3}$') 209 if p.match(stu.ID): 210 break 211 else: 212 print( "输入的有错误!") 213 while True: 214 stu.score1 = int(input("请输入学生语文成绩")) 215 if stu.score1 <= 100 and stu.score1 > 0 : 216 break 217 else: 218 print( "输入的学生成绩有错误!") 219 while True: 220 stu.score2 = int(input("请输入学生数学成绩")) 221 if stu.score2 <= 100 and stu.score2 > 0 : 222 break 223 else: 224 print( "输入的学生成绩有错误!") 225 while True: 226 stu.score3 = int(input("请输入学生英语成绩")) 227 if stu.score3 <= 100 and stu.score3 > 0 : 228 break 229 else: 230 print( "输入的学生成绩有错误!") 231 stu.sum = stu.score1 + stu.score2 + stu.score3 232 Add(stulist,stu) 233 234 if nChoose == '2': 235 ID = input("请输入学生的ID") 236 Search(stulist, ID) 237 238 if nChoose == '3': 239 ID = input("请输入学生的ID") 240 Del(stulist, ID) 241 if nChoose == '4': 242 ID = input("请输入学生的ID") 243 Change(stulist, ID) 244 245 if nChoose == '5': 246 display(stulist) 247 248 if nChoose == '6': 249 Sort(stulist) 250 251 252 if nChoose == '0': 253 break 254 255 if __name__ == '__main__': 256 stulist =[] 257 Init(stulist)
本文来自博客园,作者:一石数字欠我15w!!!,转载请注明原文链接:https://www.cnblogs.com/52-qq/p/9244226.html