Differences
This shows you the differences between two versions of the page.
python_wiki:main_program_loop [2015/03/09 23:26] billdozor |
python_wiki:main_program_loop [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 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 ===== | ||
- | |||
- | <code python> | ||
- | 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: | ||
- | menu_choice = main_menu() | ||
- | |||
- | if menu_choice == " | ||
- | # Quit from main menu. | ||
- | print(" | ||
- | break | ||
- | |||
- | else: | ||
- | # Send Menu Choice to run_program | ||
- | run_program(menu_choice) | ||
- | continue | ||
- | |||
- | #trap keyboard interrupts (control+c), | ||
- | except (KeyboardInterrupt): | ||
- | input(" | ||
- | continue | ||
- | # END OF WHILE LOOP | ||
- | return 0 | ||
- | ## End of main() ## | ||
- | </ |