subject

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.
My code is as follows:
#try except
try:
#opening the file
read_file = open('numbers. txt', 'r')
#Store the numbers in the variable file_numbers.
file_numbers = read_file. read()
#close the file
read_file. close()
#Split the number of files in list_values.
list_values = file_numbers. split()
#how many numbers are there
list_length = len(list_values)
try:
#loop it up
for i in range(list_length):
list_values[i] = float(list_values[i])
#Add up all the numbers, put into list_sum
List_sum = sum(list_values)
#heres how we average it
Average_value = (List_sum)/list_length
#print
print(Average_value)
except ValueError:
print( "File must have only numbers. Try again." )
#handles IOError exceptions
except IOError:
#Display statement
print("Trouble opening file. Try again.")y:
#opening the file
read_file = open('numbers. txt', 'r')
#Store the numbers in the variable file_numbers.
file_numbers = read_file. read()
#close the file
read_file. close()
#Split the number of files in list_values.
list_values = file_numbers. split()
#how many numbers are there
list_length = len(list_values)
try:
#loop it up
for i in range(list_length):
list_values[i] = float(list_values[i])
#Add up all the numbers, put into list_sum
List_sum = sum(list_values)
#heres how we average it
Average_value = (List_sum)/list_length
#print
print(Average_value)
except ValueError:
print( "File must have only numbers. Try again." )
#handles IOError exceptions
except IOError:
#Display statement
print("Trouble opening file. Try again.")
This code will not print the average. Unsure why. The code functioned fine before the try, except lines were added in.

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 17:00
Ineed a good science fair name i’m doing a homemade water filter and i have no idea what the title should be plz
Answers: 1
question
Computers and Technology, 22.06.2019 05:00
Pls do you believe that the use of 3d animation has grown in feature films over the last few years? if so, do you think the trend will continue? what are the forces driving this trend?
Answers: 2
question
Computers and Technology, 23.06.2019 01:30
In deadlock avoidance using banker’s algorithm, what would be the consequence(s) of: (i) a process declaring its maximum need as maximum possible for each resource. in other words, if a resource a has 5 instances, then each process declares its maximum need as 5. (ii) a process declaring its minimum needs as maximum needs. for example, a process may need 2-5 instances of resource a. but it declares its maximum need as 2.
Answers: 3
question
Computers and Technology, 23.06.2019 16:30
You have read about the beginnings of the internet and how it was created. what was the internet originally created to do? (select all that apply) share research. play games. communicate. share documents. sell toys
Answers: 1
You know the right answer?
Modify the program you wrote for Chapter 6 Exercise 6 so it handles the following exceptions:
...
Questions
Questions on the website: 13722360