Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 found

Explanation / 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)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote