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

python. Write a function, called update dictionary that has two parameters: a fi

ID: 3718446 • Letter: P

Question

python.

Write a function, called update dictionary that has two parameters: a filename and a dictionary. The function should Open the given file 1. If the file open succeeds print "filename loaded successfully." 2· lf the file open fails: print "filename does not exist." 2 Read in the 3. Provide the new size: print "The dictionary has_entries." 4 Return the updated dictionary. y:why', '18: late', 'r':'are', 'u': you The dictionary key is the text abbreviation and the value is the English translation For example, one entry in your dictionary will be, 18: late because one of the rows in the txt file contains 18 and 'late Test your function before moving on to the next question

Explanation / Answer

def update_dictionary(filename, d): try: with open(filename, 'r') as f: print(filename + ' loaded successfully.') for line in f: words = line.strip().split(",") d[words[0]] = words[1] print('The dictionary has ' + str(len(d.items())) + ' entries.') except: print(filename + ' does not exist.')