python_wiki:main_program_loop

Main Program Loop

General Information

This is an example of a main program loop, wrapped in a try command to catch keyboard interrupts.

Checklist

  • Modules required: signal (keyboard interrupts)

The Code

import signal
 
def main():
# Main program loop: Until quit is detected, ask user what to do
  while True:
    # Display Main Menu and get user input
    # wrap the "try" command to prevent a ctrl+c to break out of the program
    try:
      menu_choice = main_menu()
 
      if menu_choice == "quit":
        # Quit from main menu.
	print("Quitting program...")
	break
 
      else:
	# Send Menu Choice to run_program
	run_program(menu_choice)
	continue
 
    #trap keyboard interrupts (control+c), quit clean only
    except (KeyboardInterrupt):
      input("\nPlease quit by entering 'q' or 'quit'! [Enter] to continue:")
      continue
  # END OF WHILE LOOP	
  return 0
## End of main() ##

  • python_wiki/main_program_loop.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)