|
| 1 | +import time |
| 2 | +import os |
| 3 | +from enum import Enum |
1 | 4 |
|
| 5 | + |
| 6 | +class UserChoice(Enum): |
| 7 | + """Enum for user choices to improve code readability.""" |
| 8 | + EXIT = 1 |
| 9 | + CONTINUE = 0 |
| 10 | + |
| 11 | + |
| 12 | +def get_user_choice() -> int: |
| 13 | + """ |
| 14 | + Prompt user for input and return a valid choice (0 or 1). |
| 15 | + |
| 16 | + Returns: |
| 17 | + int: 0 to continue or 1 to exit |
| 18 | + """ |
| 19 | + while True: |
| 20 | + user_input = input("\nPlease enter your choice (1 to exit, 0 to continue): ").strip() |
| 21 | + |
| 22 | + if user_input not in ('0', '1'): |
| 23 | + print("Error: Invalid input. Please enter only 0 or 1.") |
| 24 | + continue |
| 25 | + |
| 26 | + return int(user_input) |
| 27 | + |
| 28 | + |
| 29 | +def exit_program(): |
| 30 | + """ |
| 31 | + Manage user interaction for exiting or continuing the program. |
| 32 | + """ |
| 33 | + print("Welcome to the program!") |
| 34 | + print("This program allows you to either continue or exit.") |
| 35 | + print("Enter '1' to exit or '0' to continue.") |
| 36 | + |
| 37 | + choice = get_user_choice() |
| 38 | + |
| 39 | + if choice == UserChoice.EXIT.value: |
| 40 | + _handle_exit() |
| 41 | + else: |
| 42 | + _handle_continue() |
| 43 | + |
| 44 | + # Log the action |
| 45 | + log_action(choice) |
| 46 | + |
| 47 | + |
| 48 | +def _handle_exit(): |
| 49 | + """Handle exit logic.""" |
| 50 | + print("\nYou have chosen to exit the program. Goodbye!") |
| 51 | + print("Exiting the program... Saving progress and shutting down.") |
| 52 | + time.sleep(2) # Simulate saving progress |
| 53 | + # Add cleanup code here if needed (close files, save data, etc.) |
| 54 | + |
| 55 | + |
| 56 | +def _handle_continue(): |
| 57 | + """Handle continue logic.""" |
| 58 | + print("\nYou have chosen to continue. The program will continue running.") |
| 59 | + perform_task() |
| 60 | + |
| 61 | + |
| 62 | +def perform_task(): |
| 63 | + """ |
| 64 | + Execute the next task in the program. |
| 65 | + """ |
| 66 | + print("Performing the next task...") |
| 67 | + # Add your task logic here |
| 68 | + |
| 69 | + |
| 70 | +def log_action(choice: int) -> None: |
| 71 | + """ |
| 72 | + Log user action for debugging/tracking purposes. |
| 73 | + |
| 74 | + Args: |
| 75 | + choice: The user's choice (0 or 1) |
| 76 | + """ |
| 77 | + action = "exit" if choice == UserChoice.EXIT.value else "continue" |
| 78 | + # Implement logging (file, database, etc.) |
| 79 | + # Example: logger.info(f"User chose to {action}") |
| 80 | + pass |
0 commit comments