Question
For Python: This assignment is to modify Programming Assignment 4 Grade Average to make it robust. (basically adding numbers and finding an average).In industry, a
For Python:
This assignment is to modify Programming Assignment 4 Grade Average to make it robust. (basically adding numbers and finding an average).In industry, a robust program is one that the user cannot cause an exception to occur. To do that, the programmer needs to catch all possible exceptions within the program, give an intelligent error message to the user, and allow the user to reenter the data so the program may continue. In the Grade Average program, there are two possible exceptions that can be caused by the user; the user can type in something that is not a number, e.g. 'fred', causing a ValueError, or the user can enter a negative number without entering any numbers to average causing a ZeroDivisionError. You must modify the program to use a loop any place the user enters a number to keep requesting them to enter a number until they enter a number.
You will also add the following function to your program: calculateAverage(sum, count) where sum is the sum to be averaged and count is the count of the numbers in sum. It will return the average which is simply sum/count or it will raise a ValueError if count = 0. In the main logic of the program, you will call calculateAverage to get the average but must catch the ValueError if it is raised.
You will see that by making the program robust, you will more than double the amount of code in the assignment.
Sample run:what I currently have is:
def calculateAverage(sum, count): try: avg = sum / count print("The average of the numbers is:", format(avg, ".1f")) except ValueError: if count == 0: raise ValueError except ZeroDivisionError: print("You did not enter any numbers to average.") count = 0 sum = 0 badData = True while badData: try: number = int(input("Enter a positive number to total or a negative number to calculate average:")) if number
But everytime I just enter a negative number immediately it doesn't register the ZeroDivision Error:
Please help me fix this.
Python 3.6.4 Shell File Edit Shel Debug Options Window Help Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license)"for more information RESTART: P:/Cmpsc131/Programming Assignment Solutions/PA13AverageofNumberswithExceptions.py Enter a positive number to total or a negative number to calculate average: 10 Enter a positive number to total or a negative number to calculate average: 20 Enter a positive number to total or a negative number to calculate average: up What you entered was not a valid number. Try again Enter a positive number to total or a negative number to calculate average: 30 Enter a positive number to total or a negative number to calculate average: 40 Enter a positive number to total or a negative number to calculate average: -10 The sum of the numbers is: 100.0 The average of the numbers is: 25.0 RESTART: P:/Cmpsc131/Programming Assignment Solutions/PA13AverageofNumbersWithExceptions.py Enter a positive number to total or a negative number to calculate average:-2 You did not enter any numbers to average
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started