[Pythonmac-SIG] Baffling if statement
Bob Ippolito
bob at redivi.com
Thu Sep 22 22:01:57 CEST 2005
On Sep 22, 2005, at 3:34 PM, Kirk Durston wrote:
> After spending a couple hours trying to figure out what is going
> on, I’m asking for help.
>
> Below is a short segment from a module I’ve written. Before the
> part shown below, temp and Cutoff are defined. then comes the
> following:
>
> blank='-'
> if temp>Cutoff:
> print 'temp is', temp
> print 'cutoff is', Cutoff
> print 'symbol is', symbol
> InfoCollector.append(symbol)
> else:InfoCollector.append(blank)
>
> the print out shows that temp=0.261 and Cutoff is 0.29498. Clearly
> temp is smaller than Cutoff, so it should have gone to ‘else:’ but
> it didn’t. In fact, no matter if temp is larger or smaller than
> Cutoff, it always goes through the ‘if’ segment.
Sounds like you're comparing a float and a string. Don't do that,
make sure that temp and cutoff are both float if you expect a
meaningful comparison.
To convert a string to a float, use float(aString).
To see the "programmer representation" of a variable, use the %r
format string, or the repr() function. E.g.:
print 'temp is', repr(temp)
or
print 'cutoff is %r' % (Cutoff,)
Using programmer repr[esentations] for debugging is essential, the
string representations of types can be (intentionally) ambiguous.
One of the two is almost definitely a string, so you should do temp =
float(temp) or similar.
-bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythonmac-sig/attachments/20050922/b84defc0/attachment.html
More information about the Pythonmac-SIG
mailing list