[Tutor] func-question_y_n.py

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Mar 15 23:34:34 CET 2013


On 15/03/2013 22:09, Christopher Emery wrote:
> Hello Hugo,
>
> # Defines the start of a function and its options (question)
> def question_y_n(question):
>
> #1. Ask Question from user, user enters either Yes, No or whatever
> (anything that is not Yes or No)
>      answer = input(question) # prompts the user and assigns the answer
> to var answer
>
> #2. If the user had entered anything but Yes then a while loop starts
> and becomes True (meaning it is not a Yes) so it must be either No or
> something else
>      while(answer != "Yes"):

Why the unneeded parenthesis?  What about 'yes'?

>
> #3. If the answer is anything but a No then the user is prompted with
> statement and then the question asked again
>          if answer != "No":

What about 'no'?

>
> #4. Print statement and ask question again.
>              print("Please enter Yes or No for your response!")
>              answer = input(question)
>
> #5. It is assumed that if the answer was not a Yes in the while loop
> and it was not a No in the if statement than it is assumed the only
> other choice would be a No for the else statement.
>          else:
>              answer = "No"
>              break
>      return answer
>
> answer = question_y_n("You want a drink of water? :")
> print("Your answer to the question was ", answer)
>
> This code works and produces the results that I am looking for,
> however I know there is a better way to do it and now you have me
> thinking about this again.  I just spent about 3 hours doing this
> code.
>
> I was thinking of combining the Yes and No into one check however I
> was having trouble finding resource of how to do mulitiy checks in a
> statement for while.
>
> thinking like this:
>
> def question_y_n(question):
>      answer = input(question)
>      while(answer != "Yes" Or "No"): #This is wrong code

while answer not in ('yes', 'no'):

>          print("Please enter Yes or No for your response!")
>          answer = input(question)
> or
>
> # This one does not have ability to loop intill a Yes or No is given
> so my gut tells me the one above is better or the one I have at this
> time.
>
> def question_y_n(question):
>      answer = input(question)
>      if answer == "Yes" OR "No": #this didn't work either for me
>      break
>      else
>          print("Please enter Yes or No for your response!")
>          answer = input(question)
>      return answer
>
> Sorry for this layout, I myself was getting a little dizzy reading it.
>
> Thank you for your assistance!
>
> Sincerely in Christ,
> Christopher

-- 
Cheers.

Mark Lawrence



More information about the Tutor mailing list