Program A:
Contributors: Chris Chen, Evan Han
This program uses takes all the inputted marks and calculates the average of the marks combined.
Sample Inputs and Output:
Explanation of Code
Line 12-14: Initialize the variables: "classMarks" (empty list), "classMarkAverage", and "markInput".
Line 17: A "while loop" is used to continuously iterate until a valid percent mark inputs is provided or when the user wants to calculate the average by typing "-1". "while True:" indicates that the loop will run indefinitely until a "break" statement is encountered within the program, terminating the loop.
Line 19: Using the "try" function, try to do what is in its block. In this case, it is whatever is in lines 21-22.
Line 21-22: Prompt the user to enter a 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 value in the variable "markInput".
Line 24: Using the "except" function to handle any "ValueError" in its block from the corresponding "try" function in line 19.
Line 26: Using the "print()" function, notify the user they need to enter a valid number; error from line 24. Then it will repeat the "while loop" defined on line 17.
Line 28: In sequence to the "try-catch" from lines 19 and 24, if there is no "ValueError", proceed to the next part of the program.
Line 30: Employing an "if statement", check if the user inputted the value "-1". This is supposed to be used to calculate average after the user finishes inputting the marks.
Line 32: Employing a nested "if statement" in line 30, check if the user inputted at least 2 inputs for calculating average. This can be done by using the "len()" function to check the number of items in the "classMarks" list, then comparing if it is less than 2.
Line 34-36: Notify the user of the error explained on line 32 using the "print()" function. Then it will repeat the "while loop" defined on line 17.
Line 37: An "else" statement that corresponds to line 32 continues the program (calculating the average of the marks) if there is no errors.
Line 39: Using the "sum()" function, add all of the marks from the list "classMarks". Then divide it by the length of how many marks there is which can be retrieved using the "len()" function. Using the "round()" function, round the result to 1 decimal place. Finally, store the value in the variable "classMarkAverage".
Line 42: Using the "print()" function and the variable "classMarkAverage", print the resulting class average mark.
Line 45: The "break" statement is used to exit the while loop defined in Line 17. The program will end here.
Line 47: An "elif" statement that corresponds to line 30 continues the program (adding a mark percent to the "classMarks" list) if the mark input is between 0 (inclusive) and 100 (inclusive). This can be checked using the comparative statement: "0 <= markInput <= 100".
Line 49: Using the "append()" method, add the inputted mark into the "classMarks" list.
Line 52: Using the "print()" function along with the "len()" function and variable "classMarks", print the number of marks inputted.
Line 55-56: Using the "print()" function, "join()" method for styling commas, and a "for loop" used inline with the "join" method, display the list of marks inputted. Then it will repeat the "while loop" defined on line 17.
Line 59: An "else" statement that corresponds to line 30 handles all the exceptions: if the inputted number is not within 0 (inclusive) to 100 (inclusive) and not "-1". This means the input is not valid.
Line 61: Using the "print()" function, print the error explained on line 59. Then it will repeat the "while loop" defined on line 17.
Give the program a try here: https://replit.com/@ChrisChen42/Average-Marks-A?v=1
Program B:
Contributors: Chris Chen, Evan Han
This program uses takes all the inputted marks and calculates the average of the marks combined. For this program, you need to specify how many number of marks there is.
Sample Inputs and Output:
Explanation of Code
Line 12-15: Initialize the variables: "numMarks", "classMarks" (empty list), "classMarkAverage", and "markInput".
Line 18: A "while loop" is used to continuously iterate until a valid number of marks is provided. "while True:" indicates that the loop will run indefinitely until a "break" statement is encountered within the program, terminating the loop.
Line 20: Using the "try" function, try to do what is in its block. In this case, it is whatever is in line 22.
Line 22: Prompt the user to enter the number of marks using the "input()" function. The number of marks is then converted from a "string" datatype to a integer using the "int()" function. Then store the inputted value in the variable "numMarks".
Line 24: Using the "except" function to handle any "ValueError" in its block from the corresponding "try" function in line 20.
Line 26: Using the "print()" function, notify the user they need to enter a valid number; error from line 24. Then it will repeat the "while loop" defined on line 18.
Line 28: In sequence to the "try-catch" from lines 20 and 24, if there is no "ValueError", proceed to the next part of the program.
Line 30: Employing an "if statement", check if the inputted value is smaller than 2 which means it is invalid as you need at least 2 numbers to find an average.
Line 32-34: Notify the user of the error explained on line 30 using the "print()" function. Then it will repeat the "while loop" defined on line 18.
Line 35: An "else" statement that corresponds to line 30 continues the program if there is no errors.
Line 37: The "break" statement is used to exit the while loop defined in Line 18.
Line 40: Using a "for loop" along with the "range()" function and the variable "numMarks", loop the amount of times that is defined by "numMarks".
Line 42: A nested "while loop" is used to continuously iterate until a valid percent mark inputs is provided (same as in line 18, but multiple inputs).
Line 44: Using the "try" function, try to do what is in its block. In this case, it is whatever is in line 46.
Line 46: Prompt the user to enter a 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 value in the variable "markInput".
Line 49: An "if" statement checks if the mark input is between 0 (inclusive) and 100 (inclusive). This can be checked using the comparative statement: "0 <= markInput <= 100".
Line 51: Using the "append()" method, add the inputted mark into the "classMarks" list.
Line 54: Using the "print()" function along with the "len()" function and variable "classMarks", print the number of marks inputted.
Line 57-58: Using the "print()" function, "join()" method for styling commas, and a "for loop" used inline with the "join" method, display the list of marks inputted. Then it will repeat the "for loop" defined on line 40.
Line 61: The "break" statement is used to exit the while loop defined in Line 42. The program will then continue to ask for more inputs or the program will end if it reaches the inputted number of marks; referencing the variable "numMarks".
Line 62: An "else" statement that corresponds to line 49 handles all the exceptions: if the inputted number is not within 0 (inclusive) to 100 (inclusive). This means the input is not valid.
Line 64: Using the "print()" function, print the error explained on line 62. Then it will repeat the "while loop" defined on line 42.
Line 65: Using the "except" function to handle any "ValueError" in its block from the corresponding "try" function in line 44.
Line 67: Using the "print()" function, notify the user they need to enter a valid number; error from line 65. Then it will repeat the "while loop" defined on line 42.
Line 70: After the set iterations the program leaves the "for loop" defined on line 40. Using the "sum()" function, add all of the marks from the list "classMarks". Then divide it by the length of how many marks there is which can be retrieved using the "len()" function. Using the "round()" function, round the result to 1 decimal place. Finally, store the value in the variable "classMarkAverage".
Line 73: Using the "print()" function and the variable "classMarkAverage", print the resulting class average mark.
Give the program a try here: https://replit.com/@ChrisChen42/Average-Marks-B?v=1