Generation

generate functionThu, 15 Dec 2022

Create a function that allows you to move doc to another shelf directories. It should check for: 1) if the specified shelf exists in directories and give ERROR NO SUCH KEY if it does not exist 2) if the specified doc exists in the directories and show ERROR NO SUCH VALUE if it doesn't exist 3) if both doc and shelf are in the dictionary, the function should move the value of doc to the specified shelf and make return updated directories. Note that directories is a dictionary and doc values are in lists! Example: Input: directories = { '1': ['2207 876234', '11-2'], '2': ['10006'], '3': [] } doc = '11-2' shelf = '3' Output: directories = { '1': ['2207 876234'], '2': ['10006'], '3': ['11-2'] }

def move_doc(directories, doc, shelf): if shelf in directories: if doc not in directories[shelf]: print('No such value') else: for value in directories.values(): if doc in value: value.remove(doc) else: print('No such key') directories[shelf].append(doc) return directories directories = { '1': ['2207 876234', '11-2', '5455 028765'], '2': ['10006', '5400 028765', '5455 002299'], '3': [] } doc = '11-2' shelf = '3' move_doc(directories, doc, shelf)

Questions about programming?Chat with your personal AI assistant