[Tutor] What's the error (program)

Magnus Lycka magnus@thinkware.se
Wed Nov 6 07:27:03 2002


At 03:24 2002-11-06 +0000, Alex Gillis wrote:
>OK, here is the recently annotated program thats causing problems.  I hope
>you can understand it and I haven't done anything blatantly wrong.  Feel
>free to critise anything, I haven't had any one who knows what they're doing
>look at anything I've written before so it'd be good to get some advice.

>from visual import *

Using this form of import is generally bad practice. I know that
particularly GUI toolkits use this form, but then they usually have
a specific prefix on all imported variables. Now, it's difficult
for us to see what yo have imported. If you use an IDE that can
show objects, or if you do dir(), you might see hundreds or
thousands of objects you don't need.

Please use

import visual

or

from visual import x, y, z

(where x, y and z would be the names you import of course.)

>#I guess this is the heart of the program.  While a ball is in the air, the
>else
>#statement controls its flight.  If its at either throw point then the two
>#bottom elif statements deal with it.  If its not in the air but not ready
>#to be thrown then it is referred to one of the scoop functions.
>
>def throwcatch(ball):
>     if ball.pos.y == 0:

Is this safe? Why not "ball.pos.y <= 0"?

 >>> dt = 0.1
 >>> dt-dt
0.0
 >>> dt+dt-dt-dt
0.0
 >>> dt+dt+dt-dt-dt-dt
2.7755575615628914e-017
 >>> dt+dt+dt-dt-dt-dt == 0
0
 >>> dt = 0.01
 >>> dt-dt
0.0
 >>> dt+dt-dt-dt
0.0
 >>> dt+dt+dt-dt-dt-dt
-3.4694469519536142e-018
 >>> dt+dt+dt-dt-dt-dt == 0
0

It's good rule NEVER to compare float on equality.

I think we've discussed the binary nature of floats in
programs before.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se