In python: Write a function addFileContents(filename) which will calculate the s
ID: 3668315 • Letter: I
Question
In python:
Write a function addFileContents(filename) which will calculate the sum of any numbers in the file and print the sum. A number is defined as any string beginning with a digit 0 through 9 followed by any number of digits 0 through 9. Alphanumeric strings (strings including both numbers and letters) are not to be included in the summation. You will be expected to handle any errors related to a missing file.
>>>addFileContents ("example.txt") The sum is 115 >>> addFileContents ("nosample.txt") File not foundExplanation / Answer
while True:
try
infile= input(str("please enter the name of the file"))
break
except(FileNotFoundError, IOError):
print("File not found")
def addFileContents(infile)
try
with open(infile) as filename:
for input in filename:
number = int(input)
sum= sum+number
except ValueError
print ("'{}' is not a number")
print (sum)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.