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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 03:30, mem81
How can you repin an image on your pinterest pin board a. click on the "repin" button b. click on the "add pin" button c. click on the "upload a pin" button d. click on the "save pin" button.
Answers: 2
image
Computers and Technology, 23.06.2019 18:30, DSUDLER5555
Write a program that prints the day number of the year, given the date in the form month-day-year. for example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. the program should check for a leap year. a year is a leap year if it is divisible by 4, but not divisible by 100. for example, 1992 and 2008 are divisible by 4, but not by 100. a year that is divisible by 100 is a leap year if it is also divisible by 400. for example, 1600 and 2000 are divisible by 400. however, 1800 is not a leap year because 1800 is not divisible by 400.
Answers: 3
image
Computers and Technology, 24.06.2019 10:00, wwwcarolynzouowficz
3. what do the terms multipotentialite, polymath, or scanner mean?
Answers: 2
image
Computers and Technology, 25.06.2019 01:00, lilybear1700
What phrase indicates someone has knowledge and understanding of computer, internet, mobile devices and related technologies?
Answers: 1
You know the right answer?
Modify the program you wrote for Chapter 6 Exercise 6 so it handles the following exceptions:
...

Questions in other subjects: