Modify the program you wrote for Chapter 6 Exercise 6 so it handles the following
exceptions:
• It should handle IOError exceptions that are raised when the file is opened
and data is read from it by printing "Trouble opening file. Try again." and
not executing any more of the code.
• It should handle any ValueError exceptions that are raised when the items
that are read from the file are converted to a number by printing "File must have
only numbers. Try again." and not executing any more of the code.
Code:
def main():
total = 0.0
count = 0
random_numbers_file = open(' ','r')
for line in random_numbers_file:
number = float(line)
count += 1
total += number
average = total / count
print(average)