We created three variables to store the grades obtained by a student at math, english and science.
You need to compute the average of the grades and print to the console if the user has passed or not.
Assignment
Follow the Coding Tutorial and let's practice with everything we've learned!
In this lesson, we will learn how to compute the average of grades and determine if a student has passed based on their average score. This is a fundamental concept in programming that involves basic arithmetic operations and conditional statements. Understanding how to perform these operations is crucial for solving more complex problems in the future.
Before diving into the solution, let's understand the basic concepts:
Let's start with a simple example. Suppose a student has the following grades: Math = 85, English = 78, and Science = 92. To find the average, we add these grades and divide by the number of subjects.
Here are the key concepts and techniques involved:
Let's break down the steps:
Let's consider a few examples:
# Example 1
math = 85
english = 78
science = 92
# Calculate the average
average = (math + english + science) / 3
# Check if the student has passed
if average >= 60:
print("The student has passed.")
else:
print("The student has not passed.")
In this example, the average is 85, which is above the passing threshold of 60, so the student has passed.
Here are some common mistakes to avoid:
Best practices include:
For more advanced scenarios, you might want to handle cases where grades are not available or are invalid. You can use exception handling to manage such cases.
# Example with exception handling
try:
math = 85
english = 78
science = 92
# Calculate the average
average = (math + english + science) / 3
# Check if the student has passed
if average >= 60:
print("The student has passed.")
else:
print("The student has not passed.")
except Exception as e:
print(f"An error occurred: {e}")
Here is the complete code implementation:
# Define the grades
math = 85
english = 78
science = 92
# Calculate the average
average = (math + english + science) / 3
# Check if the student has passed
if average >= 60:
print("The student has passed.")
else:
print("The student has not passed.")
To debug and test your code, you can use print statements to check the values of variables at different stages. Additionally, you can write test cases to ensure your function works correctly.
# Test case
def test_student_passed():
math = 85
english = 78
science = 92
average = (math + english + science) / 3
assert average >= 60, "Test failed: The student should have passed."
test_student_passed()
print("All tests passed.")
When approaching problems like this, break down the problem into smaller steps and solve each step one at a time. Practice with different sets of grades to get comfortable with the logic.
In this lesson, we learned how to calculate the average of grades and determine if a student has passed. This involved basic arithmetic operations and conditional statements. Mastering these concepts is essential for solving more complex problems in programming.
For further reading and practice, consider the following resources: