Contributors: Chris Chen, Alan Chen
This program uses the percent mark inputted by the user to output a letter mark.
Sample Inputs and Output:
Explanation of Code
Line 20: A "while" loop is used to continuously iterate until a valid percent mark input is provided. "while True:" indicates that the loop will run indefinitely until a "break" statement is encountered within the program, terminating the loop.
Lines 22-24: Initialize the variables: "name", "percentMark", and "letterMark".
Lines 27-28: Prompt the user to enter their name and percent mark using the "input()" function. The percent mark is then converted from a "string" datatype to a "float" using the "float()" function. Then store the inputted values in the variables "name" and "percentMark".
Line 31: Employing an "if statement", verify if the user inputted a valid value. This condition is assessed using the comparison statements: "percentMark >= 0" and "percentMark <= 100", utilizing the logical operator "and" to ensure both statements are fulfilled. If true, proceed to execute the code within the if statement block.
Lines 33-42: Using the variable "percentMark", decide the corresponding letter mark using the comparative operator "<=" - smaller or equal to. We have to start with the lowest grading section and work our way to the highest because using this comparative operator "<=" will mean if a number, for example, 0 is inputted, it will be smaller than 100, so the outputted letter mark will always be an A, which is incorrect. Therefore, we positioned the comparative statements to correspond to the percent mark in this order:
"percentMark <= 59" will set the letter mark as "F"
"percentMark <= 69" will set the letter mark as "D"
"percentMark <= 79" will set the letter mark as "C"
"percentMark <= 89" will set the letter mark as "B"
else set the letter mark as "A"
Line 45: Utilizing the "print()" function, the variable "name", "percentMark", and "letterMark" output a concatenated message with the letter mark of the user.
Line 48: The "break" statement is used to exit the while loop defined in Line 20.
Line 59: An "else" statement accompanies the "if statement" from Line 31 to handle tasks if the condition evaluates to false.
Line 51: Output an error message using the "print()" function, prompting the user to input a valid parameter for the variable "percentMark". Since there is no "break" statement here, the code will return to Line 27-28 to prompt for input again.
Give the program a try here: https://replit.com/@ChrisChen42/Convert-Percentage-Mark-to-Grade-Letter?v=1