Back

Exam score using a function - Answers

Q1. No answer needed.
Q2. After displaying an introduction, the user is asked to enter a score. Depending upon the score, one of a number of grades is displayed. They then press <ENTER> to quit.
Q3. See the Downloads section for the complete program after the answers to the questions have been applied. Change line 7 to:

if mark >= 80:

Q4. You should test a lower value e.g. 71, a border value e.g. 79, 80 or 81 and a valid value that is higher e.g. 87.
Q5. If you left out these lines, the program would return 'None' for scores of 50 or under. This would confuse a user as it isn't meaningful.
Q6. The function needs just one parameter to work.
Q7. The function outputs just one parameter.
Q8. Insert

if mark >= 95:
    return '\nYou have won a prize!\n'

just above

if mark >= 80:

Q9. insert \n in relevant places (see the program from the Downloads section).
Q10. See the final Python program in the Downloads section but here it is as well:

#How did your exams go?

another='y'

def grade(mark):
'''
This program decides how well you have done in an exam.
It needs one integer.
It returns one string, saying how well you did.
'''
if mark >= 95:
    return '\nYou have won a prize!\n'
if mark >= 80:
    return '\nPass with distinction\n'
elif mark > 60:
    return '\nPass with merit\n'
elif mark > 50:
    return '\nPass\n'
else:
    return '\nFail\n'

while another.lower() == "y":
    yourScore = int(input("\nPlease enter your score >>> "))
    print(grade(yourScore))
    another = input("\nWould you like to enter another score? Press Y or y for yes or any other key for no >>> ")

input("Press <ENTER> to quit")

Back