newbie question: if var1 == var2:

Reggie Dugard reggiedugard at sbcglobal.net
Sat Nov 29 15:54:39 EST 2008


On Fri, 2008-11-28 at 19:47 -0800, joemacbusiness at gmail.com wrote:
> Hi All,
> 
> I dont understand why the following code never finds "tree".

The problem is that the lines you are reading from the file have a
newline at the end so 'tree' != 'tree\n'.  See below for suggested
changes.

> I could not find the answer in the Python tutorials.
> Here is the code, test43.in, and runtime:
> 
> #!/usr/bin/python
> 
> fp = open("test43.in")
> var = 'tree'
> 
> for item in fp:
      item = item.strip()  # Strip off white space
      # No commas necessary at the end of the prints
      # now that we've removed the newlines.
>     print "item:", item
>     if item == var:
>         print "found tree:", item

Hope this helps you understand what was happening.  Have fun with
python!

-Reggie




More information about the Python-list mailing list