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

1 #Write a function called \"load_file\" that accepts one 2 #parameter: a filena

ID: 3705955 • Letter: 1

Question

1 #Write a function called "load_file" that accepts one 2 #parameter: a filename. The function should open the 3 #file and return the contents.# 123 5 # - If the contents of the file can be interpreted as 6 # an integer, return the contents as an integer. 7 # - Otherwise, if the contents of the file can be 8 # interpreted as a float, return the contents as a 9 # float. 10 # - Otherwise, return the contents of the file as a 11 # string. 12+ 13 #You may assume that the file has only one line. 15 #Hints: 16 17 # - Don't forget to close the file when you're done! 18 # - Remember, anything you read from a file is 19 # initially interpreted as a string. 22 #write your function here! def load_file(filename) : open file=open(filename,"r") if type(open_file)==type(2) : return int(open_file.readline()) elif type(open_file) ==type(2.3): return float(open_file.readline()) else: return str(open_file.readline()) open_file.close | 1set eturn losed 5 #Below are some lines of code that will test your function. 36 #You can change the value of the variable(s) to test your 37 #function with different inputs. 39 #If your function works correctly, this will originally 40 #print 123, followed by . 41 contents = load file("LoadFromFileInput.txt") 42 print(contents) 43 print (type contents))

Explanation / Answer

def load_file(filename): content = '' with open(filename, 'r') as f: content = f.readline() try: content = int(content) except: try: content = float(content) except: pass return content