forked from dominict/pythonteachingcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleGradeTracker.py
More file actions
73 lines (58 loc) · 2.66 KB
/
SampleGradeTracker.py
File metadata and controls
73 lines (58 loc) · 2.66 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
# Displaying the title and instructions for the user
print("-------- Weighted Course Grade Calculator --------")
print("\nInstructions: Enter the number of weighted assignments, then input each grade (0-100) and its weight (0-100). The total weight must not exceed 100. Use this calculator to determine your overall course grade, assess the impact of assignments, and track your academic progress throughout the semester.")
# Initialize an empty list to store each assignment's grade and weight
assignments = []
# Ask the user how many assignments or sections they want to enter
num = int(input("\nHow many weighted assignments/sections do you have? "))
# Track the total weight entered by the user to ensure it doesn't exceed 100
total_weight = 0
# Loop through each assignment
for i in range(num):
print("\nAssignment/Section", i + 1)
# Get a valid grade from the user (between 0 and 100)
while True:
grade = float(input(" Enter the grade (0-100): "))
if 0 <= grade <= 100:
break
else:
print(" Please enter a grade between 0 and 100.")
# Get a valid weight from the user and ensure total doesn't exceed 100
while True:
weight = float(input(" Enter the weight: "))
if 0 <= weight <= 100 and total_weight + weight <= 100:
total_weight += weight
break
else:
print(" Weight must be between 0 and 100 and total weights cannot exceed 100.")
# Store the grade and weight as a dictionary and add to the list
assignment = {"grade": grade, "weight": weight}
assignments.append(assignment)
# Function to calculate the weighted overall grade
def calculate_grade(assignments):
total = 0
for a in assignments:
# Multiply each grade by its weight and add to the total
total += a["grade"] * a["weight"]
# Check to avoid division by zero
if total_weight == 0:
print(" Weight must be between 0 and 100 and total weights cannot exceed 100.")
else:
overall = total / total_weight
return overall
# Function to return a motivational message based on the final grade
def grade_message(grade):
if grade >= 90:
return "Great job! You're doing excellent!"
elif grade >= 80:
return "Good work! Keep it up!"
elif grade >= 70:
return "You're getting there, keep pushing!"
else:
return "Don't give up! You can improve!"
# Call the calculation function and print the result and message
overall = calculate_grade(assignments)
print("\nYour current overall grade is:", overall, "%")
print(grade_message(overall))
# Closing message
print("\nThanks for using the calculator! Good luck!")