Created
December 7, 2021 19:07
-
-
Save gdvalderrama/686246a38a994b60f64700ee85af7f9b to your computer and use it in GitHub Desktop.
Script that asks a yes/no question
This file contains hidden or 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import argparse | |
def yesno(question): | |
prompt = f'{question} (y/n): ' | |
answer = input(prompt).strip().lower() | |
if answer not in ['y', 'n']: | |
print(f'{answer} is an invalid choice, please select y/n') | |
return yesno(question) | |
if answer == 'y': | |
return True | |
return False | |
def main(): | |
parser = argparse.ArgumentParser(description="""Simple script to ask a question expecting a yes or no answer.""") | |
parser.add_argument("question", type=str, nargs='?', default="Yes or No?", help="Write your question") | |
args = parser.parse_args() | |
answer = yesno(args.question) | |
print(f'Your answer was: {answer}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment