Ask coding questions

← Back to all posts
i need help
SajidVijayan3 (1)

How do I make a yes or no question using elif staments in python

Answered by heyitsmarcus (287) [earned 5 cycles]
View Answer
Commentshotnewtop
heyitsmarcus (287)

Let's say you have a variable that we'll set to "yes" called isReplCool. And then we'll test it to see whether it's a yes or a no. Now, most of the time, yes or no just refers to true or false but in this case, I'm going to take it literally and use yes or no:

isReplCool = "yes"
if isReplCool == "no":
	print("Aw, I heard it was!")
elif isReplCool == "yes":
	print("Awesome! I gotta check it out!")

And we can also do the same with true and false.

isReplCool = True
if isReplCool == False:
	print("Aw, I heard it was!")
elif isReplCool == True:
	print("Awesome! I gotta check it out!")
heyitsmarcus (287)

@SajidVijayan3 Please consider marking my answer as the accepted answer if this helped you with your problem! Happy coding!

SajidVijayan3 (1)

@heyitsmarcus so I did it and is it right
message_1 = " "
while message_1 != 'yes':
print('Are you at the base yet?") (Yes/No)')
message_1 = input()
print("Alright nice")

while message_1 != 'no':
print('Why are taking so long?") (Yes/No)')
message_1 = input()

SajidVijayan3 (1)

@SajidVijayan3 if not can you edit it and show me

heyitsmarcus (287)

@SajidVijayan3 I'm thinking that somehow upon you pasting that code, the indentation was lost. But, beyond that, even though it looks like you're checking for the correct yes and no response, capitalization is important in this instance. Me, as a user, would think to put Yes with a capital Y or No with a capital N because you told me to. But, in your code, you aren't checking for a capital Y or a capital N.

This is actually an easy fix. You want to change the case to be either uppercase or lowercase (no matter what the user puts in), and then check for that case. So, notice in the code below, I'm converting message_1 to uppercase with message_1.upper() and then checking that it's equal to YES. This way, if the user types in YeS it still counts as a yes response.

message_1 = " "
while message_1.upper() != 'YES':
  print('Are you at the base yet?") (Yes/No)')
  message_1 = input()
print("Alright nice")

while message_1.upper() != 'NO':
  print('Why are taking so long?") (Yes/No)')
  message_1 = input()

Now, when the user runs it, they can type Yes or No in any case and it will recognize it. This is important as you have to think about UX while developing, as well.