|
| 1 | +# In this series of exercises you will create a simple student database. |
| 2 | +# Before diving in, please spend a moment reading through the instructions and thinking about what |
| 3 | +# sort of data structures are necessary for organising the data stored by your program. |
| 4 | + |
| 5 | +# Part 1: adding students |
| 6 | +# First write a function named add_student, which adds a new student to the database. |
| 7 | +# Also write a preliminary version of the function print_student, which prints out the information of a single student. |
| 8 | + |
| 9 | +# These function are used as follows: |
| 10 | + |
| 11 | +# students = {} |
| 12 | +# add_student(students, "Peter") |
| 13 | +# add_student(students, "Eliza") |
| 14 | +# print_student(students, "Peter") |
| 15 | +# print_student(students, "Eliza") |
| 16 | +# print_student(students, "Jack") |
| 17 | + |
| 18 | +# Your program should now print out |
| 19 | + |
| 20 | +# Peter: |
| 21 | +# no completed courses |
| 22 | +# Eliza: |
| 23 | +# no completed courses |
| 24 | +# Jack: no such person in the database |
| 25 | + |
| 26 | +# Part 2: adding completed courses |
| 27 | +# Please write a function named add_course, which adds a completed course to the information of a specific student in the database. |
| 28 | +# The course data is a tuple consisting of the name of the course and the grade: |
| 29 | + |
| 30 | +# students = {} |
| 31 | +# add_student(students, "Peter") |
| 32 | +# add_course(students, "Peter", ("Introduction to Programming", 3)) |
| 33 | +# add_course(students, "Peter", ("Advanced Course in Programming", 2)) |
| 34 | +# print_student(students, "Peter") |
| 35 | + |
| 36 | +# When some courses have been added, the information printed out changes: |
| 37 | + |
| 38 | +# Peter: |
| 39 | +# 2 completed courses: |
| 40 | +# Introduction to Programming 3 |
| 41 | +# Advanced Course in Programming 2 |
| 42 | +# average grade 2.5 |
| 43 | + |
| 44 | +# Part 3:brepeating courses |
| 45 | +# Courses with grade 0 should be ignored when adding course information. Additionally, if the course is already in the database in that specific |
| 46 | +# student's information, the grade recorded in the database should never be lowered if the course is repeated. |
| 47 | + |
| 48 | +# students = {} |
| 49 | +# add_student(students, "Peter") |
| 50 | +# add_course(students, "Peter", ("Introduction to Programming", 3)) |
| 51 | +# add_course(students, "Peter", ("Advanced Course in Programming", 2)) |
| 52 | +# add_course(students, "Peter", ("Data Structures and Algorithms", 0)) |
| 53 | +# add_course(students, "Peter", ("Introduction to Programming", 2)) |
| 54 | +# print_student(students, "Peter") |
| 55 | + |
| 56 | +# Peter: |
| 57 | +# 2 completed courses: |
| 58 | +# Introduction to Programming 3 |
| 59 | +# Advanced Course in Programming 2 |
| 60 | +# average grade 2.5 |
| 61 | + |
| 62 | +# Part 4: summary of database |
| 63 | +# Please write a function named summary, which prints out a summary based on all the information stored in the database. |
| 64 | + |
| 65 | +# students = {} |
| 66 | +# add_student(students, "Peter") |
| 67 | +# add_student(students, "Eliza") |
| 68 | +# add_course(students, "Peter", ("Data Structures and Algorithms", 1)) |
| 69 | +# add_course(students, "Peter", ("Introduction to Programming", 1)) |
| 70 | +# add_course(students, "Peter", ("Advanced Course in Programming", 1)) |
| 71 | +# add_course(students, "Eliza", ("Introduction to Programming", 5)) |
| 72 | +# add_course(students, "Eliza", ("Introduction to Computer Science", 4)) |
| 73 | +# summary(students) |
| 74 | + |
| 75 | +# This should print out |
| 76 | + |
| 77 | +# students 2 |
| 78 | +# most courses completed 3 Peter |
| 79 | +# best average grade 4.5 Eliza |
| 80 | + |
| 81 | +def add_student(students, name): |
| 82 | + students[name] = {} |
| 83 | + |
| 84 | +def add_course(students, name, course): |
| 85 | + if course[1] == 0: |
| 86 | + return |
| 87 | + if course[0] in students[name]: |
| 88 | + if course[1] > students[name][course[0]]: |
| 89 | + students[name][course[0]] = course[1] |
| 90 | + else: |
| 91 | + students[name].update({course[0]:course[1]}) |
| 92 | + |
| 93 | +def summary(students): |
| 94 | + print(f"students {len(students)}") |
| 95 | + most_courses = ["", 0] |
| 96 | + best_average = ["", 0] |
| 97 | + for key, value in students.items(): |
| 98 | + if len(value) > most_courses[1]: |
| 99 | + most_courses = [key, len(value)] |
| 100 | + avg = sum(value.values())/len(value) |
| 101 | + if avg > best_average[1]: |
| 102 | + best_average = [key, avg] |
| 103 | + print(f"most courses completed {most_courses[1]} {most_courses[0]} ") |
| 104 | + print(f"best average grade {best_average[1]} {best_average[0]} ") |
| 105 | + |
| 106 | + |
| 107 | +def print_student(students, name): |
| 108 | + if name not in students: |
| 109 | + print(f"{name}: no such person in the database") |
| 110 | + return |
| 111 | + print(f"{name}:") |
| 112 | + if students[name] == {}: |
| 113 | + print(" no completed courses") |
| 114 | + else: |
| 115 | + print(f" {len(students[name])} completed courses:") |
| 116 | + values_sum = 0 |
| 117 | + for key, value in students[name].items(): |
| 118 | + print(f" {key} {value}") |
| 119 | + values_sum += value |
| 120 | + print(f" average grade {values_sum / len(students[name])}") |
0 commit comments