Generation

generate functionThu, 15 Dec 2022

Refine the function below to handle errors 1) if shelf variable is not found in directories, it will show ERROR NO SUCH KEY 2) if doc variable is not found in directories, it will show ERROR NO SUCH VALUE def change_shelf(data): docnum = '11-2' shelf = '3' for key, val in data.items(): for doc in val: if doc == docnum: data[shelf].append(doc) data[key].remove(doc) print('OK') return data directories = { '1': ['2207 876234', '11-2'], '2': ['10006'], '3': [] }

def change_shelf(data, docnum, shelf): if not shelf in data: return'ERROR NO SUCH KEY' for val in data.values(): if docnum in val: val.append(docnum) print('OK') return data return 'ERROR NO SUCH VALUE' directories = { '1': ['2207 876234', '11-2'], '2': ['10006'], '3': [] }

Questions about programming?Chat with your personal AI assistant