[Tutor] func-question_y_n.py

Christopher Emery cpe.list at gmail.com
Fri Mar 15 22:01:50 CET 2013


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


More information about the Tutor mailing list