Question: Can you please modify my code so that it handles any IOError exceptions that are raised when the file is opened and the data is
Can you please modify my code so that it handles any IOError exceptions that are raised when the file is opened and the data is read from it And so that it handles any ValueError exceptions that are raised when the items that are read from the file are converted to a number. import random
# get the number of random numbers from the user
numbersneeded intinputEnter the number of random numbers to generate:
# this part opens the file to write in:
with opennumberstxtw as file:
# Generate and write random numbers to the file
for in rangenumbersneeded:
num random. randint
file.writestrnum
printfnumbersneeded random numbers have been written to numbers.txt
#this second section reads the numbers from the file and displays the data
# here the file is opened in read mode
with opennumberstxt as file:
numbers file. readnes
# displaying the numbers
printRandom Numbers:"
for num in numbers:
printnumstrip
# this part sums the total of random numbers
total float, numbers
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
