How do I make a yes or no question using elif staments in python
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
false but in this case, I'm going to take it literally and use
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!")
@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
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
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
No in any case and it will recognize it. This is important as you have to think about UX while developing, as well.