Question: Im getting an error Coding Problem 4.4.2 (Exteriai Tes0ul 0 Averaging.py 1 Write a function called average file. average file should 2 thave one parameter:
Coding Problem 4.4.2 (Exteriai Tes0ul 0 Averaging.py 1 Write a function called average file. average file should 2 thave one parameter: a filename. 3# 4 The file should have an integer on each line. average file 5 should return the average of these integers. However, if 6 any of the lines of the file are _not_ integers, 7 Haverage file should return the string Error reading file!" 8# 9 WRemember, by default, every time you read a line from a 10 file, it's interpreted as a string. 12 13 #Add your function here! 14 def average file(filename) : 15 16 17 18 19 20 21 try: with open (filename, "as f total- count 0 for line in f: try: totalint(line.strip()) count+1 23 24 25 26 27 28 29 30 #8elow are some lines of code that will test your function. 31 You can change the value of the variable(s) to test your 32 ffunction with different inputs. except: return "Error reading file!" return total / count except Peturn "Error reading e 34 If your function works correctly, this will originally 35 #print: 5.0, then Error reading file! 36 37 You can select valid-file.txt and invalid-file.txt from 3the dropdown in the top left to preview their contents. 39 print (average file("valid file.txt") 40 print (average file("invalid file.txt")) 41
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
