#!/usr/bin/env python # # Author : Ahmed Obied (amaobied@ucalgary.ca) # Date : Jan. 17th 2008 # Purpose : Calculates the percentage and letter grade based on a given # total and mark # # boolean to control the loop end_loop = False while not end_loop: # get the total and make sure it's greater than zero total_marks = float(raw_input('Enter the total number of marks: ')) if total_marks <= 0: print 'Error: The number must be greater than zero.' else: # if we get a number greater than zero then we stop iterating end_loop = True end_loop = False while not end_loop: # get the mark and make sure it's greater than zero mark = float(raw_input('Enter your mark : ')) # check if the mark is in the proper range if mark < 0 or mark > total_marks: print 'Error: incorrect input' else: end_loop = True # calculate the percentage value percentage = (mark * 100.0) / total_marks # find the letter grade based on the computed percentage if percentage < 39.0: letter_grade, letter_sign = 'F', ' ' elif percentage < 44.0: letter_grade, letter_sign = 'D', '-' elif percentage < 49.0: letter_grade, letter_sign = 'D', ' ' elif percentage < 54.0: letter_grade, letter_sign = 'D', '+' elif percentage < 59.9: letter_grade, letter_sign = 'C', '-' elif percentage < 64.0: letter_grade, letter_sign = 'C', ' ' elif percentage < 69.0: letter_grade, letter_sign = 'C', '+' elif percentage < 74.0: letter_grade, letter_sign = 'B', '-' elif percentage < 79.0: letter_grade, letter_sign = 'B', ' ' elif percentage < 84.0: letter_grade, letter_sign = 'B', '+' elif percentage < 89.0: letter_grade, letter_sign = 'A', '-' else: letter_grade, letter_sign = 'A', ' ' # write the output to the console with proper formatting print 'You got %.2f%c on the test which gives you a letter grade of %c%c' % \ (percentage, '%', letter_grade, letter_sign)