[Tutor] func-question_y_n.py

Mitya Sirenef msirenef at lightbird.net
Fri Mar 15 22:28:29 CET 2013


On 03/15/2013 05:01 PM, Christopher Emery wrote:
> Hello All,
 >
 > Okay, I have created a small function that will check to see if a user
 > has answered with a Yes, No or other response. If the user puts yes
 > or no the function ends, if they put anything but yes or no then the
 > function will ask them the same question and tell them they either
 > need to put yes or no for their response
 >
 > This is what I have as of now:
 > ### Start of Code ###
 > def question_y_n(question):
 > answer = input(question)
 > while(answer != "Yes"):
 > if answer != "No":
 > print("Please enter Yes or No for your response!")
 > answer = input(question)
 > else:
 > answer = "No"
 > break
 > return answer
 >
 > answer = question_y_n("You want a drink of water? :")
 > print("Your answer to the question was ", answer)
 > ### End of Code ###
 >
 > Is this the best way to handle this? I would like to use this any
 > time I have a yes or no question. In the future I will add to this and
 > make it work with yes/no, true/false, 1/0 so that anytime there is
 > anything that only has two answer I could use this instead of writing
 > more code for each two answer type questions.
 >
 > Thank you for your advice ahead of time!!!
 >
 > PS: I hope don't mind me posting code that works to see if I can 
improve on it!
 >
 > Sincerely in Christ,
 > Christopher


Hi Christopher,

I've recently made a couple of functions that do this in a more general
way. There are some usage examples at the end, including y/n input. The
difference between them is that the 2nd function adds more options.



import re

def simpleinp(pattern, prompt="> ", convert=None, errmsg="Invalid 
Input", blank=False):
     """Keep asking user for input until it matches `pattern`."""
     if pattern == "%d": pattern = "\d+"; convert = int
     if pattern == "%s": pattern = ".+"

     while True:
         i = input(prompt).strip()
         if blank and not i: return None

         if re.match('^'+pattern+'$', i):
             return convert(i) if convert and i else i
         else:
             print(errmsg)


def getinp(pattern, prompt="> ", convert=None, errmsg="Invalid Input", 
ignorecase=False, lower=False, blank=True):
     """Keep asking user for input until it matches `pattern`."""
     if pattern == "%d":
         pattern = "\d+"
         convert = int
     if pattern == "%f":
         pattern = "\d+.?\d*"
         convert = float
     if pattern == "%s":
         pattern = "\S+"

     while True:
         i = input(prompt).strip()
         if blank and not i:
             return None
         if lower:
             i = i.lower()
         flags = re.I if ignorecase else 0

         if re.match('^'+pattern+'$', i, flags=flags):
             return convert(i) if convert else i
         else:
             print(errmsg)


# print( getinp("%d", "integer: ") )
# print( getinp("%f", "float: ") )
# print( getinp("%s", "string: ") )
# print( getinp("(y|n)", "y/n: ", lower=True) )

# print( simpleinp("%d", "integer: ") )
# print( simpleinp("%s", "string: ") )
# print( simpleinp(".*", "string or blank: ") )
# print( simpleinp("[ynYN]", "y/n: ") )

HTH,  -m



-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/

The dreamer can know no truth, not even about his dream, except by awaking
out of it.  George Santayana



More information about the Tutor mailing list