lesson to learn

  • Thread starter David R. Stockwell (wg-xiao)
  • Start date
D

David R. Stockwell (wg-xiao)

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
 
I

Irmen de Jong

David said:
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
 
L

Leif K-Brooks

David said:
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:?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top