-1

I am using a very simple and basic if elif in python but it only will give me the same result no mater what my input is.

answer = input('Do you like christmas? ')

if answer == 'no' or 'No':
    print('Your so uncool, just leave, this is a place for festive people you 
    heartless monster')


elif answer == 'yes' or 'Yes':
    print('Your pretty cool, you can stay, just dont get drunk on the eggnog and 
    make out with the dog...')


elif 'brit' or 'Brit' in answer:
    print('I was gunna be mean to brit, but its christmas and I am not allowed 
    because a fat man in a red suit will reck me')


else:
    print('MAKE UP YOUR MIND, YOU SUCK')

the outputs are place holders but at the moment all I get when I run this is the the print for the no answer, weather I answer no yes or anything else...

edit: I see now that in the question my indenting looks really terrible, that isn't how I wrote it but that is just from posting it here, please assume that indentation is correct.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • 2
    Possible duplicate of [How do I test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-do-i-test-multiple-variables-against-a-value) – Carcigenicate Dec 24 '17 at 03:31
  • 1
    `if answer.lower() == 'no'` but you should also learn the logic operator precidence causing your problem – f5r5e5d Dec 24 '17 at 03:36

2 Answers2

0

You are getting this because your first if statement is actually being parsed like this:

if (answer == 'no') or ('No'):

Which means if either answer == 'no' or 'No' is considered "truthy", then the statement will go ahead. Any non-empty string in python is considered to be true so you will always get True in your first statement.

bool('')
# will output False
bool('No')
# will output True

If you actually want to perform this, you've got to run the comparison every time...

if answer == 'no' or answer == 'No':

However, there are some cleaner ways of doing this, such as

answer.lower() == 'no'
# or, if you want lots more examples
answer in ['no', 'No', 'NOPE', 'Not at all', 'Never']

Would recommend reading up

SCB
  • 5,821
  • 1
  • 34
  • 43
0

Your problem is with the if's statements:

answer = 'yes'
if answer == 'no' or 'No':
               1. ^ (this is saying, 'yes' == 'no' or 'No')
               2. ^ (this is saying, False or 'No')
               3. ^ (this is saying, False or True)
               4. ^ (this is saying True)

In python strings are truthy, meaning that they are interpreted as true in boolean statements.

So your correct statement (and for the others):

answer = 'yes'
if answer == 'no' or answer == 'No':
               1. ^ (this is saying, 'yes' == 'no' or 'yest == ''No')
               2. ^ (this is saying, False or False)
               3. ^ (this is saying False)

So that's it! Oh, another tip, you can compare any type of no/yes this way

answer = str.lower(input('Do you like christmas? '))
if answer == 'no':
   ^ Even if the user types 'No', it will be turned into 'no'.
Mateus Mercer
  • 46
  • 1
  • 7