-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadwriteappendgrades.py
90 lines (81 loc) · 2.76 KB
/
readwriteappendgrades.py
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
import math
class Class:
def __init__(self, subject, grades):
self.subject = subject
self.grades = grades
def main():
# Create an empty list to store the subjects.
subjects = []
# Read the grades from the file.
with open("grades.txt", "r") as f:
grades = f.readlines()
# Print the menu.
print("Welcome to your Grade Calculator")
print("1. New Entry")
print("2. Highest Course Average")
print("3. Lowest Course Average")
print("4. Calculate your Average")
print("5. Exit")
# Get the user's choice.
choice = int(input("Enter your choice: "))
# Process the user's choice.
while choice != 5:
if choice == 1:
# Add a new class.
subject = input("Enter the name of the new subject: ")
subjects.append([])
for i in range(5):
grade = float(input("Enter the grade for {}: ".format(subject)))
subjects[-1].append(grade)
with open("grades.txt", "a") as f:
f.write("{}: {}\n".format(subject, subjects[-1]))
elif choice == 2:
# Find the class with the highest average grade.
max_sort = -1
max_avg_grade = -1.0
for i in range(len(subjects)):
sum = 0.0
for j in range(len(subjects[i])):
sum += subjects[i][j]
avg_grade = sum / len(subjects[i])
if avg_grade > max_avg_grade:
max_sort = i
max_avg_grade = avg_grade
if max_sort == -1:
print("No classes found.")
else:
print("Class with highest average grade: " + subjects[max_sort])
elif choice == 3:
# Find the class with the lowest average grade.
min_sort = -1
min_avg_grade = 101.0
for i in range(len(subjects)):
sum = 0.0
for j in range(len(subjects[i])):
sum += subjects[i][j]
avg_grade = sum / len(subjects[i])
if avg_grade < min_avg_grade:
min_sort = i
min_avg_grade = avg_grade
if min_sort == -1:
print("No classes found.")
else:
print("Class with lowest average grade: " + subjects[min_sort])
elif choice == 4:
# Calculate the average grades for each subject.
for i in range(len(subjects)):
sum_of_grades = sum(subjects[i])
average_grade = sum_of_grades / len(subjects[i])
print("The average grade for {} is {}".format(subjects[i], average_grade))
else:
print("Invalid choice.")
print("Welcome to your Grade Calculator")
print("1. New Entry")
print("2. Highest Course Average")
print("3. Lowest Course Average")
print("4. Calculate your Average")
print("5. Exit")
# Get the user's choice.
choice = int(input("Enter your choice: "))
if __name__ == "__main__":
main()