167k views
5 votes
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.

1 Answer

8 votes

Answer:

#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.")

Step-by-step explanation:

The python program uses the try-except exception handling method to catch errors in the code. The IOError is used for catching input and output errors in the program like file handling while the ValueError keyword in the except statement is used to catch data type errors from a return statement, calculation or user input.

User James An
by
4.0k points