[Tutor] func-question_y_n.py

Dave Angel davea at davea.name
Fri Mar 15 23:31:51 CET 2013


On 03/15/2013 06:09 PM, Christopher Emery wrote:

------ Hugo said:
----
----1. ask a question, receive an answer
----2. if the answer is "Yes" or "No", return the answer
----3. else, print a message and go back to step 1


> 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"):

Hugo's function description is perfectly symmetric with regard to Yes & 
No.  But you've changed that, both in your new description, and in your 
code.  Why is the "while" checking only for "Yes" when it should be 
checking for either/both?


>   <SNIP>
>
> 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
>          print("Please enter Yes or No for your response!")
>          answer = input(question)
> or


Do you understand about compound if/while expressions?  You can combine 
boolean expressions with 'and' and 'or' .

For example, if you had wanted to check for either 4 or 12, you might do 
something like:
     if myint == 4 or myint == 12:
            do something
     else:
            do something else



-- 
DaveA


More information about the Tutor mailing list