def access(db,key,field): '''Return the filler of the field in db[key], if there is one; otherwise, return None''' if field in db[key]: return db[key][field] else: return None def accessWithPrint(db,key,field): '''Return the filler of the field in db[key], if there is one; otherwise, return None''' print("") print("==>Just called accessWithPrint") print("key=",key,"field=",field) print("db[key] on the next line:") print(" ",db[key]) if field in db[key]: print("db[key][field]=",db[key][field]) return db[key][field] else: return None def pp(first,last,books): print(first,last,end="") if books: print(" is the author of the following books:") for b in books: print(b) else: print(" is not an author") def main(): db = { 'jgoodall' : {'surname' : 'Goodall',\ 'forename' : 'Jane',\ 'born' : 1934,\ 'died' : None,\ 'notes' : 'primate researcher',\ 'author' : ['In the Shadow of Man', 'The Chimpanzees of Gombe']},\ 'rfranklin' : {'surname' : 'Franklin',\ 'forename' : 'Rosalind',\ 'born' : 1920,\ 'died' : 1957,\ 'notes' : 'contributed to discovery of DNA'},\ 'rcarson' : {'surname' : 'Carson',\ 'forename' : 'Rachel',\ 'born' : 1907,\ 'died' : 1964,\ 'notes' : 'raised awareness of effects of DDT',\ 'author' : ['Silent Spring']}} print("====Printing all the authors:") for k in db: if 'author' in db[k]: pp(db[k]['forename'],db[k]['surname'],db[k]['author']) else: pp(db[k]['forename'],db[k]['surname'],[]) print("=========") print('jgoodall was born in',access(db,'jgoodall','born')) print('jgoodall was born in',accessWithPrint(db,'jgoodall','born')) print('jgoodall wrote these books',accessWithPrint(db,'jgoodall','author')) print('rfranklin wrote these books',accessWithPrint(db,'rfranklin','author')) main()