Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if string is "affirmative"?

Tags:

python

I'd like to use Python 3's input() function to ask the user a yes or no question. For example:

affirmative = ["", "y", "yes"]
x = input("Continue? [Y/n] ").lower()
if x in affirmative:
    # Do something
else:
    print("Canceled")

Is there a certain list I should be using for "affirmative" answers? I know Python has things like string.uppercase and string.lowercase for pre-determined lists of variables. Is there a list of answers that should be considered a "yes" answer?

like image 884
Lucas Phillips Avatar asked Dec 21 '25 09:12

Lucas Phillips


1 Answers

Python's distutils has strtobool which might work for you. Documentation Link.

distutils.util.strtobool(val)

Convert a string representation of truth to true (1) or false (0).

  • True values are: y, yes, t, true, on, 1.
  • False values are: n, no, f, false, off, 0.
  • Raises ValueError otherwise.

But this doesn't exactly fit with the style you're currently hoping for.

like image 199
Bill Lynch Avatar answered Dec 22 '25 21:12

Bill Lynch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!