1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
| def Main_Show(): print('\n') print(' ==========欢迎使用==========') print(' 1、管理员登录') print(' 2、教师登录') print(' 3、学生登录') print(' 4、退出程序') print('============================')
def Admin_Show(): print(' ==========欢迎你管理员==========') print(' 1、修改管理员用户名和密码 ') print(' 2、增加教师') print(' 3、增加学生') print(' 4、删除教师') print(' 5、删除学生') print(' 6、返回上一层') print(' ================================')
def Teacher_Show(): print(' ==========欢迎你老师==========') print(' 本学期:2023-2024 第一学期') print(' 1、查询学生') print(' 2、录入成绩') print(' 3、修改成绩') print(' 4、查询所有学生成绩') print(' 5、返回上一层') print(' ================================')
def Stundet_Show(): print(" ==========欢迎你使用==========") print(" 本学期:2023-2024 第一学期") print(" 1、查询成绩") print(" 2、返回上一层") print(" ================================")
def Modify_Admin_User(): New_Admin_User = input("请设置新的管理员用户名:") New_Admin_Password = input("请设置新的管理员密码: ")
with open("AdminUsernameAndPassword.txt","w") as file: file.write(f"管理员用户名:{New_Admin_User}\n") file.write(f"管理员密码:{New_Admin_Password}\n")
def Admin_Login(): Admin_User = input("请输入用户名:") Admin_Login_Password = input("请输入密码:") if Admin_User == Admin and Admin_Login_Password == Admin_Password: return True else: print("管理员用户名或密码错误") return False
def Teacher_Login(): Teacher_User = input("请输入用户名:") Teacher_Login_Password = input("请输入密码:") print("登录成功!")
def Student_Login(): Student_User = input("请输入用户名") Student_Login_Password = input("请输入密码") print("登录成功! ")
def Add_TeacherUser(): New_TeacherUser = input("请输入新的用户:") New_TeacherUser_Password = input("请输入" + New_TeacherUser + "的登录密码")
with open("TeacherUsernameAndPassword.txt", "a") as file: file.write(f"教师的用户名:{New_TeacherUser}\n") file.write(f"教师的密码:{New_TeacherUser_Password}\n") print("添加老师成功")
def Add_Student(): New_Student = input("请输入新的用户:") New_Student_Password = input("请输入" + New_Student + "的登陆密码")
with open("StudentUsernameAndPassword.txt", "a") as file: file.write(f"{New_Student} \n") file.write(f"学生的密码:{New_Student_Password}\n") print("添加学生成功")
def Del_Student_User(): Student_User = input("请输入要删除的学生:") Lines = []
with open("StudentUsernameAndPassword.txt", "r") as file: Lines = file.readlines()
Deleted = False with open("StudentUsernameAndPassword.txt", "w") as file: for Line in Lines: if Student_User in Line: Deleted = True else: file.write(Line)
if Deleted: print("用户 " + Student_User + " 已被删除。") print("已从 StudentUsernameAndPassword.txt 中删除包含 " + Student_User + " 的行。")
def Del_Teacher_User(): Del_Teacher_User = input("请输入要删除的老师:") Lines = []
with open("TeacherUsernameAndPassword.txt", "r") as file: Lines = file.readlines()
Deleted = False with open("TeacherUsernameAndPassword.txt", "w") as file: for Line in Lines: if Del_Teacher_User in Line: Deleted = True else: file.write(Line)
if Deleted: print("用户 " + Del_Teacher_User + " 已被删除。") print("已从 TeacherUsernameAndPassword.txt 中删除包含 " '{Del_Teacher_User}' " 的行。")
def Print_StudentInfo(): with open("StudentUsernameAndPassword.txt", "r") as file: Lines = file.readlines()
print("名单:") Flag = 1 for Line in Lines: if "密码" not in Line: Student_Name = Line.strip() print(f"{Flag}. {Student_Name}") Flag += 1
def Input_Student_Achievements(): with open("StudentUsernameAndPassword.txt", "r") as file: Lines = file.readlines()
print("以下是所有学生的列表:") Flag = 1 Students = [] for Line in Lines: if "密码" not in Line: Student_Name = Line.strip() Students.append(Student_Name) print(f"{Flag}. {Student_Name}") Flag += 1
while True: Choice = input("请输入要录入成绩的学生编号(输入'全部'录入所有学生的成绩,输入'完成'结束选择):") if Choice == "全部": for Student in Students: Enter_Grades(Student) print("所有学生成绩录入完成。") break elif Choice == "完成": break else: try: index = int(Choice) - 1 if 0 <= index < len(Students): Student = Students[index] Enter_Grades(Student) print(f"{Student} 的成绩录入完成。") else: print("输入的学生编号无效,请重新输入。") except ValueError: print("输入的学生编号无效,请重新输入。")
def Enter_Grades(Student): Subjects = ["计算机网络原理", "数据库系统原理", "C语言", "离散数学", "数据结构"] Grades = {} with open("StudentAchievements.txt", "r") as file: Existing_Students = file.readlines() for Line in Existing_Students: if Line.strip() == Student: print("学生已存在") return
for Subject in Subjects: Grade = input(f"请输入 {Student} 的 {Subject} 成绩:") Grades[Subject] = Grade
with open("StudentAchievements.txt", "a") as file: file.write(f"{Student} \n") for Subject, Grade in Grades.items(): file.write(f"{Subject}: {Grade}\n")
print("成绩录入完成")
def Update_Student_Grades(): with open("StudentUsernameAndPassword.txt", "r") as file: Lines = file.readlines()
print("以下是所有学生的列表:") Flag = 1 Students = {} for Line in Lines: if "密码" not in Line: Student = Line.strip() Students[Flag] = Student print(f"{Flag}. {Student}") Flag += 1
while True: Choice = input("请输入要修改成绩的学生编号:") if Choice.isdigit(): Choice = int(Choice) if Choice in Students: Student_name = Students[Choice]
print(f"当前 {Student_name} 成绩如下:") with open("StudentAchievements.txt", "r") as file: Lines = file.readlines() start_index = 0 for i, Line in enumerate(Lines): if Line.strip() == Student_name: start_index = i + 1 break
for Line in Lines[start_index:]: if ": " in Line: print(Line.strip()) else: break
Course_Name = input("请输入要修改成绩的课程名称:")
Grade_Line_index = 0 for i, Line in enumerate(Lines): if Line.strip().startswith(Course_Name + ": "): Grade_Line_index = i break else: print("未找到指定课程的成绩行。") continue
Current_Grade = Lines[Grade_Line_index].strip() print(f"当前成绩: {Current_Grade}")
new_Grade = input(f"请输入{Student_name}的新成绩:") Lines[Grade_Line_index] = f"{Course_Name}: {new_Grade}\n"
with open("StudentAchievements.txt", "w") as file: file.writelines(Lines)
print(f"{Student_name}的 {Course_Name} 成绩已更新为{new_Grade}!") break else: print("无效的学生编号,请重新输入。") else: print("无效的输入,请重新输入。")
def Find_All_Student(): Student_Grades = {}
with open("StudentAchievements.txt", "r") as file: Lines = file.readlines()
Current_Student = None for Line in Lines: Line = Line.strip() if Line == "": continue
if ":" not in Line: Current_Student = Line.strip() Student_Grades[Current_Student] = {} elif Current_Student: Parts = Line.split(":") Course = Parts[0].strip() Grade = float(Parts[1].strip()) Student_Grades[Current_Student][Course] = Grade
Courses = sorted(list(set([Course for Grades in Student_Grades.values() for Course in Grades.keys()])))
Header = "学生姓名\t" + "\t".join(Courses) print(Header)
for Student,Grades in Student_Grades.items(): Line = f"{Student}\t\t" Line += "\t\t".join([str(Grades.get(Course,0.0)) for Course in Courses]) print(Line)
def Find_Student_Grades(): Student_Name = input("请输入学生姓名:") with open("StudentAchievements.txt", "r") as file: Lines = file.readlines() Found = False for i, Line in enumerate(Lines): if Line.strip() == Student_Name: Found = True print(f"{Student_Name}的成绩如下:") for j in range(i + 1, len(Lines)): if ":" in Lines[j]: Subject, Grade = Lines[j].strip().split(": ") print(f"{Subject}: {Grade}") else: break break if not Found: print("未找到该学生的成绩信息。")
print('程序为第一次使用,请设置管理员用户名和密码!') Admin = input(('请设置管理员用户名:')) Admin_Password = input(('请设置管理员密码:')) with open("AdminUsernameAndPassword.txt", "w") as file: file.write(f"管理员用户名:{Admin}\n") file.write(f"管理员密码:{Admin_Password}")
while True: Main_Show() Main_Choice = input('请按键选择:')
if Main_Choice == '4': print('==========欢迎再次使用==========') break if Main_Choice == '1': if Admin_Login(): while True: Admin_Show() Admin_Choice = input("请按键选择:") if Admin_Choice == '1': Modify_Admin_User() elif Admin_Choice == '2': Add_TeacherUser() elif Admin_Choice == '3': Add_Student() elif Admin_Choice == '4': Del_Teacher_User() elif Admin_Choice == '5': Del_Student_User() elif Admin_Choice == '6': break elif (Admin_Choice > '7' or Admin_Choice < '0'): print("请输入1-6按键")
if Main_Choice == '2': Teacher_Login() while True: Teacher_Show() Teacher_Choice = input("请按键选择:") if(Teacher_Choice == '1'): Print_StudentInfo() print("登录成功!") if(Teacher_Choice == '2'): Input_Student_Achievements() if(Teacher_Choice == '3'): Update_Student_Grades() if(Teacher_Choice == '4'): Find_All_Student() if(Teacher_Choice == '5'): break elif(Teacher_Choice > '5' or Teacher_Choice < '0'): print("请输入1-5按键")
if Main_Choice == '3': Student_Login() while True: Stundet_Show() Student_Chocie = input("请按键选择:") if(Student_Chocie == '1'): Find_Student_Grades()
if(Student_Chocie == '2'): break elif(Student_Chocie > '3' or Student_Chocie < '0'): print("请输入1-2按键")
|