Question: Each of the following pieces of python code attempt to compute the sum of numbers read from a file. For each one, indicate whether it

Each of the following pieces of python code attempt to compute the sum of numbers read from a file. For each one, indicate whether it is correct; if it is not, explain precisely what is wrong with it. (For example, if it has a syntax error, describe which line, or part of a line, is incorrect; if it results in a runtime error, explain where and why the error happenst runs but prints the wrong value, give a concrete example.) Note that any number (zero, one, two, three or all four) of them may be correct. You should assume that the file numbers.txt exists, and that it is a text file containing one (integer) number per line (a) def sum file(file) total = 0 for line in file: total total int(line.strip)) return total print("The sum is", sum file("umbers.txt")) (b) file -open ("numbers.txt") total = 0 while line in file: total = total + int (file. readline()) file.close() print("The sum is", total) (c) def sum file(filename): fileopen(filename) try: return sum(file.readlines )) finally file.close print("The sum is", sum file("numbers.txt")) (d) file open("numbers.txt") total = 0 for line in file: total total + int (file.readline()) = file.close print("The sum is", total) Also, giving a correct code which can compute the sum of numbers read from a file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
