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, 22.06.2019 10:00
According to alisa miller foreign news bureaus
Answers: 3
question
Computers and Technology, 22.06.2019 21:00
Kirk found a local community college with a two-year program and he is comparing the cost with that of an out-of-state two-year school. what is the expected total cost for one year at the local community college if kirk lives at home? what is the expected total cost for one year at the out-of-state school if kirk lives on campus?
Answers: 2
question
Computers and Technology, 22.06.2019 22:30
Which of the following factors would your hypothetical supervisor look at when deciding whether to test a biological material sample for dna? the amount of other evidence you have implicating a suspect in a crime the annual budget for the crime lab both of the above none of the above; you would almost always order a test
Answers: 3
question
Computers and Technology, 22.06.2019 23:00
Which factor is the most important when choosing a website host? whether customers will make secure transactions the number of email accounts provided the purpose of the website the quality of the host control panel
Answers: 3
You know the right answer?
Modify the program you wrote for Chapter 6 Exercise 6 so it handles the following exceptions:
...
Questions
question
Mathematics, 06.07.2019 16:10
question
Mathematics, 06.07.2019 16:10
Questions on the website: 13722359