Unknown model· unknown
from NVIDIA HelpSteer3 Preference
give me python code to ask for confirmation interactively in a CLI program
- User
give me python code to ask for confirmation interactively in a CLI program
- Assistant
Sure, I'd be happy to help you with that! Here's a Python function that you can use to ask for confirmation interactively in a CLI (Command Line Interface) program. This function uses the
input()function to get user input and checks if the input is one of the expected confirmation values (e.g., 'yes', 'y', 'no', 'n'). If the input is not recognized, the function will prompt the user again until a valid input is provided.pythondef ask_for_confirmation(prompt: str = "Are you sure you want to continue? [yes/no] ") -> bool: """ Ask for confirmation interactively in a CLI program. :param prompt: The prompt message to display to the user. :return: True if the user confirms, False otherwise. """ while True: user_input = input(prompt).lower() if user_input in ['yes', 'y']: return True elif user_input in ['no', 'n']: return False else: print("Sorry, I didn't understand your response. Please enter 'yes' or 'no'.")Here's an example of how to use this function in your CLI program:
pythonif ask_for_confirmation(): print("User confirmed, proceeding...") # Perform the action that requires confirmation else: print("User cancelled, aborting...")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.