I don\'t understand this problem. I wrote some code out, but it\'s the most basi
ID: 674347 • Letter: I
Question
I don't understand this problem. I wrote some code out, but it's the most basic part of the code. This problem needs to be solved using Python 3.
This is the text inside the Squares.txt file:
I'm not sure how I am suppose to do this. I wrote this code so far:
I am a beginner at Python coding, so please make the code simple and easy to understand. I want to understand how the code is running.
This chapter deals with Files and Exceptions. You need to know how to read, write files, as well as handling exceptions, not only processing data, but open file for reading and writing. For this chapter assignment, just catch exceptions and keep handling the rest. You need list that exception both in your output file as well as console what you found, line number or place that you find a problem, and gnored. Submit a zip file that contain all your input file, as well as your code for easy grading. Also, for better inform user about the problem encountered during input file process, print out error, exception, message to the console. Along with the line number in the input file for easy allocation when user checking the input file (User Interface Issue).Explanation / Answer
import math
myfile = open('squares.txt','r')
writeFile = open('SqrtResult','w')
lines = myfile.readlines()
for line in lines:
try:
print 'Square root of '+line.strip()+' is '+str(math.sqrt(float(line)))
writeFile.write('Square root of '+line.strip()+' is '+str(math.sqrt(float(line)))+' ')
except ValueError as err:
print 'Unexpected Value.Ignoring this for evaluation.'
writeFile.write('Unexpected Value.Ignoring this for evaluation. ')
myfile.close()
writeFile.close()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.