|
| 1 | +# Let's revisit the course grading project from the previous section. |
| 2 | + |
| 3 | +# As we left if last time, the program read and processed files containing student information, completed exercises and exam results. |
| 4 | +# We'll add a file containing information about the course. An example of the format of the file: |
| 5 | + |
| 6 | +# Sample data |
| 7 | + |
| 8 | +# name: Introduction to Programming |
| 9 | +# study credits: 5 |
| 10 | +# The program should then create two files. There should be a file called results.txt with the following contents: |
| 11 | + |
| 12 | +# Sample data |
| 13 | +# Introduction to Programming, 5 credits |
| 14 | +# ====================================== |
| 15 | +# name exec_nbr exec_pts. exm_pts. tot_pts. grade |
| 16 | +# pekka peloton 21 5 9 14 0 |
| 17 | +# jaana javanainen 27 6 11 17 1 |
| 18 | +# liisa virtanen 35 8 14 22 3 |
| 19 | +# The statistics section is identical to the results printed out in part 3 of the project. |
| 20 | +# The only addition here is the header section. |
| 21 | + |
| 22 | +# Additionally, there should be a file called results.csv with the following format: |
| 23 | + |
| 24 | +# Sample data |
| 25 | +# 12345678;pekka peloton;0 |
| 26 | +# 12345687;jaana javanainen;1 |
| 27 | +# 12345699;liisa virtanen;3 |
| 28 | +# When the program is executed, it should look like this: |
| 29 | + |
| 30 | +# Sample output |
| 31 | +# Student information: students1.csv |
| 32 | +# Exercises completed: exercises1.csv |
| 33 | +# Exam points: exam_points1.csv |
| 34 | +# Course information: course1.txt |
| 35 | +# Results written to files results.txt and results.csv |
| 36 | + |
| 37 | +# That is, the program only asks for the names of the input files. All output should be written to the files. The user will only see a message confirming this. |
| 38 | + |
| 39 | +student_info = input("Student information: ") |
| 40 | +exercise_data = input("Exercises completed: ") |
| 41 | +exam_points = input("Exam points: ") |
| 42 | +course_info = input("Course information: ") |
| 43 | + |
| 44 | +def create_dict(file): |
| 45 | + new_dict = {} |
| 46 | + with open(file) as new_file: |
| 47 | + for line in new_file: |
| 48 | + line = line.strip() |
| 49 | + parts = line.split(";") |
| 50 | + if parts[0] == "id": |
| 51 | + continue |
| 52 | + new_dict[parts[0]] = [] |
| 53 | + for item in parts[1:]: |
| 54 | + if item.isdigit(): |
| 55 | + new_dict[parts[0]].append(int(item)) |
| 56 | + else: |
| 57 | + new_dict[parts[0]].append(item) |
| 58 | + return new_dict |
| 59 | + |
| 60 | + |
| 61 | +def results_txt(student_names, student_exercises, student_exams, course_info): |
| 62 | + with open(course_info) as new_file: |
| 63 | + info = [] |
| 64 | + for line in new_file: |
| 65 | + line = line.replace("\n", "") |
| 66 | + parts = line.split(": ") |
| 67 | + info.append(parts[1]) |
| 68 | + content = [] |
| 69 | + content.append(f"{info[0]}, {info[1]} credits\n") |
| 70 | + content.append("======================================\n") |
| 71 | + content.append(f"{'name':30}{'exec_nbr':10}{'exec_pts.':10}{'exm_pts.':10}{'tot_pts.':10}{'grade':10}\n") |
| 72 | + for id, name in student_names.items(): |
| 73 | + if id in student_exercises: |
| 74 | + grade = calculate_grade(sum(student_exercises[id]), sum(student_exams[id])) |
| 75 | + full_name = f"{name[0]} {name[1]}" |
| 76 | + content.append(f"{full_name:30}{grade[0]:<10}{grade[1]:<10}{grade[2]:<10}{grade[3]:<10}{grade[4]:<10}\n") |
| 77 | + with open("results.txt", "w") as my_file: |
| 78 | + for item in content: |
| 79 | + my_file.write(item) |
| 80 | + |
| 81 | +def results_csv(student_names, student_exercises, student_exams): |
| 82 | + content = [] |
| 83 | + for id, name in student_names.items(): |
| 84 | + if id in student_exercises: |
| 85 | + grade = calculate_grade(sum(student_exercises[id]), sum(student_exams[id])) |
| 86 | + full_name = f"{name[0]} {name[1]}" |
| 87 | + content.append(f"{id};{full_name};{grade[4]}\n") |
| 88 | + with open("results.csv", "w") as my_file: |
| 89 | + for item in content: |
| 90 | + my_file.write(item) |
| 91 | + |
| 92 | + |
| 93 | +def calculate_grade(exercises, exam_points): |
| 94 | + point_exerc = int((exercises/40) * 10) |
| 95 | + total_points = point_exerc + exam_points |
| 96 | + grade = 5 |
| 97 | + if total_points <= 14: |
| 98 | + grade = 0 |
| 99 | + elif total_points <= 17: |
| 100 | + grade = 1 |
| 101 | + elif total_points <= 20: |
| 102 | + grade = 2 |
| 103 | + elif total_points <= 23: |
| 104 | + grade = 3 |
| 105 | + elif total_points <= 27: |
| 106 | + grade = 4 |
| 107 | + return [exercises, point_exerc, exam_points, total_points, grade] |
| 108 | + |
| 109 | +student_names = create_dict(student_info) |
| 110 | +student_exercises = create_dict(exercise_data) |
| 111 | +student_exams = create_dict(exam_points) |
| 112 | +results_txt(student_names, student_exercises, student_exams, course_info) |
| 113 | +results_csv(student_names, student_exercises, student_exams) |
0 commit comments