Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legit python if statement?

Tags:

python

I am new to Python and trying to build a practice project that reads some XML. For some reason this if statement is triggered even for blank whitespace lines:

if '<' and '>' in line:

Any ideas why?

like image 214
startuprob Avatar asked Apr 26 '11 13:04

startuprob


2 Answers

Your code says (parethesised for clarity):

if '<' and ('>' in line):

Which evaluates to:

if True and ('>' in line):

Which in your case evaluates to true when you don't intend to. So, try this instead (parenthesis are optional, but added for clarity):

if ('<' in line) and ('>' in line):
like image 147
Sander Marechal Avatar answered Oct 04 '22 20:10

Sander Marechal


You probably want:

if ('<' in line) and ('>' in line):

Your version is being interpreted as this:

if ('<') and ('>' in line):

which is probably not what you meant.

Use parenthesis to make things super-obvious.

like image 21
payne Avatar answered Oct 04 '22 21:10

payne