To locate the error in your program, the most effective approach is:
Use print statements, so you can locate the error.
This option allows you to examine which conditions are being met and to check the flow of the program as it evaluates each conditional. By adding print statements before each conditional check, you can see the values being evaluated and determine why both "Your Grade is D" and "Your Grade is F" are being printed for the input of 65.
In this specific case, the issue arises from the fact that the conditions are not mutually exclusive, and as a result, multiple conditions may be evaluated as true. The correct condition for grading should use elif
(else if) statements to prevent subsequent conditions from being evaluated once a true condition is found. Here's a revised version of your grading code:
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")
elif avg < 0 or avg > 100:
print("Invalid Input!")
This structure will ensure that only the first matching grade condition will execute.