grade-calc
grade-calc
This is a basic example to help you get started with Markdown formatting.
Headings
Use # for headings. Add more # for smaller headings:
Lists
Unordered List
Item 1
Item 2
Sub-item 2.1
Sub-item 2.2
Ordered List
1. First item
2. Second item
3. Third item
Emphasis
Bold text: **Bold**
Italic text: *Italic*
Strikethrough: ~~Strikethrough~~
Links
Click here to visit Google
Images
Code Blocks
Inline code: Use `backticks` for inline code
Block of code:
def calculate_average(grades): """ Calculates the average of a list of numerical grades.
What: This function takes a list of numbers (representing grades) as input and
returns their average. It handles the case of an empty list.
Theory: The average of a set of numbers is calculated by summing all the numbers
and dividing by the count of the numbers. This function utilizes the built-in
`sum()` function to efficiently calculate the sum of the grades and the `len()`
function to get the number of grades. Handling the empty list case prevents
a ZeroDivisionError.
"""
if not grades:
return 0 # Return 0 if the list of grades is empty
return sum(grades) / len(grades)
def assign_grade(average_score): """ Assigns a letter grade based on the average score.
What: This function takes a numerical average score as input and returns a letter
grade (A, B, C, D, or F) based on predefined grading criteria.
Theory: This function implements a series of conditional statements (`if`, `elif`, `else`)
to map numerical ranges to letter grades. These conditions define the boundaries
for each grade. The order of these conditions is important to ensure correct
grade assignment (e.g., checking for 'A' before checking for 'B').
"""
if average_score >= 90:
return "A"
elif 80 <= average_score < 90:
return "B"
elif 70 <= average_score < 80:
return "C"
elif 60 <= average_score < 70:
return "D"
else:
return "F"
def process_student_grades(student_name, grades): """ Processes a student's grades, calculates the average, and
assigns a final grade.
What: This function takes a student's name (string) and a list of their grades (numbers)
as input. It calculates the average grade and then determines the final letter grade.
Theory: This function orchestrates the use of the `calculate_average()` and `assign_grade()`
functions. It first calls `calculate_average()` to get the numerical average
of the provided grades. Then, it passes this average to `assign_grade()` to
obtain the corresponding letter grade. This demonstrates the concept of modularity
in programming, where complex tasks are broken down into smaller, reusable functions.
"""
average = calculate_average(grades)
final_grade = assign_grade(average)
print(f"\n--- Grade Report for {student_name} ---")
print(f"Grades: {grades}")
print(f"Average Grade: {average:.2f}") # Format to two decimal places
print(f"Final Grade: {final_grade}")
print("------------------------------------")
if name == "main": student1_name = "Alice" student1_grades = [85, 92, 78, 95, 88]
process_student_grades(student1_name, student1_grades)
student2_name = "Bob"
student2_grades = [65, 70, 55, 80, 60]
process_student_grades(student2_name, student2_grades)
student3_name = "Charlie"
student3_grades = [98, 99, 100]
process_student_grades(student3_name, student3_grades)