Last active
July 22, 2024 13:03
-
-
Save garrettdreyfus/8153571 to your computer and use it in GitHub Desktop.
Dead simple python function for getting a yes or no answer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def yes_or_no(question): | |
reply = str(raw_input(question+' (y/n): ')).lower().strip() | |
if reply[0] == 'y': | |
return True | |
if reply[0] == 'n': | |
return False | |
else: | |
return yes_or_no("Uhhhh... please enter ") |
#!/usr/bin/python3
def confirm_prompt(question: str) -> bool:
reply = None
while reply not in ("y", "n"):
reply = input(f"{question} (y/n): ").lower()
return (reply == "y")
reply = confirm_prompt("Are you sure?")
print(reply)
Edit: added keyword argument to set either yes or no by default
#!/usr/bin/python3
def prompt(question: str, default=None) -> bool:
choices = ("", "y", "n") if default in ("yes", "no") else ("y", "n")
hint = "Y/n" if default == "yes" else "y/n"
hint = "y/N" if default == "no" else hint
reply = None
while reply not in choices:
reply = input(f"{question} ({hint}): ").lower()
return (reply == "y") if default != "yes" else (reply in ("", "y"))
reply = prompt("Are you sure?")
print(reply, "\n")
reply = prompt("Are you sure?", default="yes")
print(reply, "\n")
reply = prompt("Are you sure?", default="no")
print(reply, "\n")
@gyoza necrothreading this just to kudos you on the fact that you quoted Meatloaf
Stop using raw_input, that's Python 2.
How would I add an --yes or -y parser arg to @icamys solution?
first
parser.add_argument("--yes", "-y", help="Always accept")
and then what? :D sorry I am really new to python
maybe something like:
def confirm(question, default_no=True):
choices = ' [y/N]: ' if default_no else ' [Y/n]: '
default_answer = 'n' if default_no else 'y'
reply = str(input(question + choices)).lower().strip() or default_answer
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
if args.yes:
default_answer='y'
else:
return False if default_no else True
but that doesnt work
@willbelr, Thank you for this beautiful & elegant solution! I modified with casefold (in Python3) but am otherwise using as-is in a few scripts, and it works great!
def confirm_prompt(question: str) -> bool:
reply = None
while reply not in ("y", "n"):
reply = input(f"{question} (y/n): ").casefold()
return (reply == "y")
reply = confirm_prompt("Are you sure?")
print(reply)
My version using simple recursion:
def user_confirm(question: str) -> bool:
reply = str(input(question + ' (y/n): ')).lower().strip()
if reply[0] == 'y':
return True
elif reply[0] == 'n':
return False
else:
new_question = question
if "Please try again - " not in question:
new_question = f"Please try again - {question}"
return user_confirm(new_question)
if __name__ == "__main__":
print(user_confirm("Do you love me?"))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much mate, I didn't know that we can return a function that I was manipulating the code in quite different ways. It changed my many lines of stuck code to refactoring.