Dispatch tables, or rather dictionaries.
You also stick keys. menu selection values for functions that perform these options:
def AddRecordHandler(): print("added") def DeleteRecordHandler(): print("deleted") def CreateDatabaseHandler(): print("done") def FlushToDiskHandler(): print("i feel flushed") def SearchHandler(): print("not found") def CleanupAndQuit(): print("byez") menuchoices = {'a':AddRecordHandler, 'd':DeleteRecordHandler, 'c':CreateDatabaseHandler, 'f':FlushToDiskHandler, 's':SearchHandler, 'q':CleanupAndQuit} ret = menuchoices[input()]() if ret is None: print("Something went wrong! Call the police!") menuchoices['q']()
Do not forget to confirm your entry! :)
Michael Foukarakis
source share