Suppose you are writing a program to convert numeric grades to letter grades:

print("Enter numeric grade: ")
avg = int(input())
if avg >= 90 and avg <= 100:
print("Your Grade is A")
if avg >= 80 and avg < 90:
print("Your Grade is B")
if avg >= 70 and avg < 80:
print("Your Grade is C")
if avg >= 60 and avg < 70:
print("Your Grade is D")
if avg >= 0 and avg < 70:
print("Your Grade is F")
if avg < 0 or avg > 100:
print("Invalid Input!")
When you test the program for the value 65, you get the following output:

Enter numeric grade:

65

Your Grade is D

Your Grade is F



** Process exited - Return Code: 0 **

Press Enter to exit terminal

How can you locate the error?

(1 point)
Responses

Use a trace tool to go line by line and see how the program is progressing.
Use a trace tool to go line by line and see how the program is progressing.

Use print statements, so you can locate the error.
Use print statements, so you can locate the error.
Use print statements, so you can locate the error.

Use a different editor, and see if that fixes the problem.
Use a different editor, and see if that fixes the problem.

Use other data points, and see if they work.
Use other data points, and see if they work.

1 answer

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:

  1. Immediate Feedback: Adding print statements will let you track the flow of the program and see what conditions are being evaluated as true.

  2. 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.

  3. 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".