python_wiki:main_program_loop

This is an old revision of the document!


Main Program Loop

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

Modules required:

  • signal (keyboard interrupts)
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.1416453513.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)