forked from inglesp/python-data-structure-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexam_results.py
More file actions
94 lines (80 loc) · 2.19 KB
/
Copy pathexam_results.py
File metadata and controls
94 lines (80 loc) · 2.19 KB
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
# This program interactively asks about your exam marks, and reports the
# corresponding grades.
#
# Usage:
#
# $ python exam_results.py
#
# A session with the program might look like the following:
#
# $ python exam_results.py
# What marks did you get in Maths?
# > 63
# What marks did you get in Philosophy?
# > 54
# What marks did you get in Geography?
# > 71
# What marks did you get in Music?
# > 68
#
# Your grades:
#
# Maths: B
# Philosophy: C
# Geography: A
# Music: B
subjects = []
grade_boundaries = {
'A': [70, 100],
'B': [60, 69],
'C': [50, 59],
'D': [40, 49],
'E': [30, 39],
'F': [0, 29],
}
student_marks = {}
student_grades = {}
def main():
get_subjects()
get_marks()
get_grades()
print_grades()
def get_marks():
for subject in subjects:
while True:
mark = input(f"What marks did you get in {subject}? ")
if validate_mark(mark, subject):
break
def validate_mark(mark, subject):
try:
mark = int(mark)
if 0 <= mark <= 100:
student_marks[subject] = mark
return True
else:
print("Please enter a number between 0 and 100.")
return False
except ValueError:
print("Please enter a valid number.")
return False
def get_grades():
for subject, mark in student_marks.items():
for grade, boundary in grade_boundaries.items():
if boundary[0] <= mark <= boundary[1]:
student_grades[subject] = grade
break
def print_grades():
print("\nYour grades:\n\n" + "\n".join(f"{subject}: {grade}" for subject, grade in student_grades.items()))
def get_subjects():
num_of_subjects = int(input("How many subjects have you taken? "))
for i in range(num_of_subjects):
subject = input(f"Subject {i + 1}: ")
subjects.append(subject)
if __name__ == "__main__":
main()
# TODO:
# * Implement the program as described in the comments at the top of the file.
# TODO (extra):
# * Modify the program to handle the case where the user enters an invalid mark
# (such as 150, or -25.5, or "bananas")
# * Modify the program to ask the user for the subjects that they've taken.