Yes or no answers


I’m using raw_input()
and I only want a yes or a no as an answer
would there be a way to do it with if, elif else?
by maybe defining a variable as yes and another one as no and the else could print ‘please use yes or no’

please help me :sob:


this is certainly possible. instead of elif you could also use and in the if condition

store the result of raw_input in a variable, then compare this variable with yes and no string.


if Q1 == N :
print Q2
elif Q1 == Y:
print Q2
print ‘please use yes or no’

I tried somethhing like that but it is still printing Q2 even if the answer wasnt yes or no ?


what is the rest of the code?

why are N and Y variables?


I define

N= 'no'
Y = 'yes'
Q1 = raw_input ' first question'
Q2 = raw_input ' second question'
if Q1 == N :
print Q2
elif Q1 == Y:
print Q2
print ‘please use yes or no’


bit overkill to use variables for that, but okay.

raw_input is a function, calling a function requires parentheses:




>>> def yesorno(prompt):
	while True:
		answer = raw_input(prompt+' ')
		if answer.lower() in 'yesno': return answer
		print ("Please answer 'yes' or 'no'.")

>>> print (yesorno("Is it raining outside?"))
Is it raining outside? yup
Please answer 'yes' or 'no'.
Is it raining outside? yes


I will try to do it like that thank you


Thank you :slight_smile: