469,903 Members | 1,651 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

lesson to learn

Hi,

I spent a couple hours today trying to track down this bug

It went something like this

def fcn(day):
print "day is ", day
if (day < 10):
print "day is less than 10"
else:
if day < 20 :
print "day is less than 20 but greater than 10"
fcn(14)

It never fell into the < 20 but > 10 loop in my 'code from my file'

but if i ran interactively it always did.

And thats what drove me crazy until I came up with this check
as a replacement:

instead of if day < 10:

i tried

if (10 - day) > 0:
blah......

And when I did that I got an exception immediately saying I couldn't
compare a string to an int.

And that was when I remembered, in my code (file version) there was a point
where under certain circumstances I was setting day to "N/A" and indeed it
was really a string.

In the file version this was just one function of a 600 line file. So its
almost like I could have saved myself the trouble by either using some sort
of hungarian notation or perhaps make some rules, or just always do checks
like that to catch little sneaky errors.....

David
Jul 18 '05 #1
2 1277
David R. Stockwell (wg-xiao) wrote:
def fcn(day):
you could add the following line here:

assert type(day) is int, "day must be int"

or:
if type(day) is not int:
raise TypeError("day must be int")
print "day is ", day
if (day < 10):
print "day is less than 10"
else:
if day < 20 :
print "day is less than 20 but greater than 10"


--Irmen
Jul 18 '05 #2
David R. Stockwell (wg-xiao) wrote:
def fcn(day):
print "day is ", day
if (day < 10):
print "day is less than 10"
else:
if day < 20 :
print "day is less than 20 but greater than 10"


Why not use elif: instead of else: if:?
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Rob | last post: by
19 posts views Thread by Onideus Mad Hatter | last post: by
4 posts views Thread by Jaga | last post: by
reply views Thread by Jaga | last post: by
reply views Thread by Joe Mayo | last post: by
31 posts views Thread by anand devarajan | last post: by
65 posts views Thread by Chris Carlen | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.