Yes or no answers


#1

Hi,
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:


#2

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.


#3

if Q1 == N :
print Q2
elif Q1 == Y:
print Q2
else:
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 ?


#4

what is the rest of the code?

why are N and Y variables?


#5

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
else:
print ‘please use yes or no’

#6

bit overkill to use variables for that, but okay.

raw_input is a function, calling a function requires parentheses:

function_name('argument')

#7

Consider,

>>> 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
yes
>>> 

#8

I will try to do it like that thank you


#9

Thank you :slight_smile: