0

The following script is returning false, when I think that it should be returning true. Any idea what is going on here? Thanks so much, guys!

test=['Pop']
test1='Pop'

if (test==('POP' or 'Pop' or 'pop' or ['POP'] or ['Pop'] or ['pop'])):    
    print "yes"
else:
    print "no"

Currently, the output is, 'no'.

9
  • 6
    You aren't comparing two lists, you are comparing a list to a string. Commented Jun 27, 2012 at 17:32
  • 5
    if test == 'POP' or '...' or '...' isn't doing what you think it is: possibly have a look in the fine documentation how boolean logic works Commented Jun 27, 2012 at 17:34
  • To ignore case of a string, you can convert both operands to lowercase. 'POP'.lower() == 'poP'.lower() Commented Jun 27, 2012 at 17:35
  • 1
    Are you sure? ['Pop'] == ['Pop'] is True Commented Jun 27, 2012 at 17:38
  • 2
    Then I strongly suggest reading the Python tutorial - if you're struggling and don't understand what everyone's said - you're going to run into serious problems later on Commented Jun 27, 2012 at 17:46

5 Answers 5

7

You aren't understanding how python processes the statement. Python isn't natural language.

if (test==(['Pop'] or 'Pop')):

Because the or is inside the parens, it processes it first. So it looks at

['Pop'] or 'Pop'

Since ['Pop'] is considered True, python reduces the whole statement to:

if (test==['Pop']):

At this point, it tests whether test is equal to ['Pop']

What you are actually wanting to do is:

(test == ['Pop']) or (test == 'Pop')

This is completely different than

 test == (['Pop'] or 'Pop')
Sign up to request clarification or add additional context in comments.

4 Comments

Or more traditionally written as test in (['Pop'], 'Pop')
@JonClements, I don't know about traditionally, but yes that's better.
Thank you. This was exactly my confusion. Thank you for being able to identify this despite the shifting code above.
@Atticus29: also note that unless you're using your own class which has it's own comparison methods, equality tests would evaluate to False between objects of different types no matter what their contents are.
2

If you write it like this, the if statement will be true when you use test or test1:

test=['Pop']
test1='Pop'
if (test in ('POP', 'Pop', 'pop', ['POP'], ['Pop'], ['pop'])):
    print "yes"
else:
    print "no"

You're basically creating a big tuple with all the possibilities: three strings and three lists. If your variable is present in there, the if statement is true.

5 Comments

I believe if any(test == i for i in ('POP', 'Pop', 'pop', ['POP'], ['Pop'], ['pop'])): would also work.
@JAB, yes, but why would ever want to do it that way?
This doesn't answer the question.
this is the right way of doing it, but it would better to explain what's wrong with the OP's approach.
@WinstonEwert So you can utilize a generator expression just for the heck of it. As an aside, Simeon didn't need to use parentheses with the if statement in C/Java style; if test in ('POP', 'Pop', 'pop', ['POP'], ['Pop'], ['pop']): has the same result.
1

test == (a or b) is different from test == a or test == b.

(a or b) returns a iff bool(a) is True and b otherwise. Therefore test == ('POP' or whatever) is equivalent to test == 'POP' because bool('POP') is True as for any other non-empty string in Python.

To test multiple values you could use: value in [a, b].

Note: ['a'] != 'a' – the later is a string while the former is a list that contains a string.

In your case you could test[0].lower() == 'pop'.

Comments

0
test=['Pop']
test1='Pop'
if test1 == 'POP' or test1 == 'Pop' or test1 == 'pop' or test1 == ['POP'] or test1 == ['Pop'] or test1 == ['pop']:
    print "yes"

else:
    print "no"

Each one is a separate test.

3 Comments

This is the closest answer to your question. The other ones are saying something completely different.
This always results in if test1 == 'POP'
This is just completely wrong. It will always say "yes" regardless of what's in test1
0

it looks like the user is just trying to do a case insensitive comparison:

if test1.lower() == 'pop':
    print 'yes'
else:
    print 'no'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.