161k views
3 votes
I can write the main program but how do i add the invalid code.

Given the following test scores and grade equivalents, write a function which is passed a score, and returns a letter grade based on the score entered. It should also check for invalid values (a number less than 0 or greater than 100) and return an 'I' in that case.

User FellowMD
by
7.2k points

1 Answer

5 votes

Final answer:

To handle invalid test scores in a grading function, include a conditional check within the function that identifies if the score falls outside the acceptable range of 0 to 100 and returns an 'I' for invalid scores. The function should then continue to check and return the appropriate letter grade for valid scores.

Step-by-step explanation:

To write a function that returns a letter grade based on a score while also validating the score, you will need to include a conditional check in your function. The validation will ensure that the score is within the acceptable range of 0 to 100. If the score is outside this range, the function will return an 'I' to indicate an invalid value. The function below demonstrates how you can achieve this in a programming language like Python:


def get_grade(score):
if score < 0 or score > 100:
return 'I'
elif score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'F'

# Example usage:
print(get_grade(85)) # Output: B
print(get_grade(-5)) # Output: I

Remember to replace the grade thresholds and letter grades with the ones specified in your assignment.

User Rishabh Mahatha
by
7.3k points