To locate the error in the program that causes it to print both "Your Grade is D" and "Your Grade is F" when the input is 65, the best approach is to use print statements, so you can locate the error.
Here’s why this is the most effective choice:
-
Immediate Feedback: Adding print statements will let you track the flow of the program and see what conditions are being evaluated as true.
-
Clarifying Logic: It allows you to understand how the program is deciding which grade to print based on the input. By printing values and conditions, you can identify that both conditions for D and F are being met.
-
Simplicity: Using print statements is straightforward and doesn't require special tools or changes in your development environment.
To fix the logic in your grade assignment, you can modify the if-statements to use elif
(else if) for mutually exclusive conditions. Here’s how the corrected code might look:
print("Enter numeric grade: ")
avg = int(input())
if avg >= 90 and avg <= 100:
print("Your Grade is A")
elif avg >= 80 and avg < 90:
print("Your Grade is B")
elif avg >= 70 and avg < 80:
print("Your Grade is C")
elif avg >= 60 and avg < 70:
print("Your Grade is D")
elif avg >= 0 and avg < 60:
print("Your Grade is F")
else:
print("Invalid Input!")
Now, when you enter 65, it will correctly only print "Your Grade is D".