newbie question: if var1 == var2:

John Machin sjmachin at lexicon.net
Sun Nov 30 00:23:49 CET 2008


On Nov 29, 2:53 pm, joemacbusin... at gmail.com wrote:
> Hi All,
>
> I dont understand why the following code cannot find the
> variable "tree".  It is very simple but I could not find the answer
> to this on the Python Tutorials.  Here is the code, input and runtime:
>
> #!/usr/bin/python
>
> fname = open("test43.in")
> var = 'tree'
>
> for item in fname:
>     print "item: ", item,

The repr() built-in function is your friend. Having trouble with "item
== var"? Do this:
      print repr(var), repr(item)
and you'll see immediately why item != var

>     if item == var:
>         print "found tree: ", item,

HTH,
John



More information about the Python-list mailing list