Python Learning Lab Assignment: Loops, If Statements, and Functions
Project Start Link
Objective
In this lab you will practice while loops, if statements, and functions by writing a modular program that works with student grades. By the end of this lab, you should be able to:
- Write and call functions to organize code.
- Use loops and conditionals to control program flow.
- Create a structured program that repeats tasks until the user decides to quit.
Program Overview
You will create a grade management program that allows a user to:
- Enter grades for multiple students (stop by typing
-1). - Keep a running total of the grades as they are entered.
- Calculate the average grade.
- Find the highest and lowest grades.
- Count how many students earned A, B, C, D, or F.
- Repeat the whole process as many times as the user wants.
Part 1: Functions
Task 1: Collecting a Single Grade
Function Name: get_grade
- Use a loop to prompt for a grade.
- Only accept numbers between
0and100. - If the user enters something invalid, ask again until it is valid.
- Return the valid grade.
Part 2: Processing Grades
Task 2: Report Counts by Grade Category
Function Name: report_grade_counts
Parameters: a_count, b_count, c_count, d_count, f_count
- Print the number of students in each category.
- Use this grading scale:
- A = 90–100
- B = 80–89
- C = 70–79
- D = 60–69
- F = below 60
Task 3: Calculate the Average
Function Name: calculate_average
Parameters:
total_sum: the sum of all gradesnum_students: how many grades were entered
Returns: the average as a float.
Part 3: Main Program
In your main function:
- Input Grades:
- Ask for grades until the user enters
-1. - As each valid grade is entered, immediately add it to the running total.
- Update counts for A, B, C, D, and F.
- Track the highest and lowest grades.
- Ask for grades until the user enters
- Calculate Statistics:
- Use the running total and number of grades to calculate the average.
- Make sure you correctly keep track of highest and lowest grades.
- Display Results:
- Print counts for each grade category.
- Print average, highest, and lowest grades.
- Repeat:
- Ask if the user wants to enter another set of grades.
- If yes, start over.
Part 4: Testing
Test your program with the following cases:
Test 1: Basic Input
85, 92, 78, -1
Expected:
Average: 85.00
Highest: 92
Lowest: 78
A’s: 1, B’s: 1, C’s: 1, D’s: 0, F’s: 0
Code language: HTTP (http)
Test 2: Boundary Values
Enter grades like 90, 89, 70, 60, 59 to check category edges.
Test 3: Invalid Input
Enter invalid grades (e.g., -5, 105) and confirm the program asks again.
Submission
Submit your Python file to OnlineGDB and Moodle.
Your program must include at the top (as comments):
- Name
- Program description
- Last modified time
- Academic Integrity Statement
Example:
# Name: Alan Turing
# Email: turing@computerscience.edu
# Last Modified: 22 February 2025, 9:15 pm
# Program Description: This program collects student grades and reports averages and categories.
# Academic Integrity Statement: I attest that I followed the academic honesty policy of this course.
# I wrote this program myself and did not use unauthorized help.
Code language: PHP (php)
If the integrity statement is missing, the lab will not be graded.
Grading Rubric (100 points)
| Requirement | Points |
|---|---|
| All features implemented | 40 |
| Code organized into functions | 10 |
| Clear formatting | 5 |
| Good variable & function names | 7 |
| Required comments (header + functions) | 8 |
| Program repeats until user quits | 5 |
| Correct output on all tests | 25 |
Total: 100
