[Tutor] Trouble with exercise regarding classes

Andrew Martin amartin7211 at gmail.com
Wed Aug 25 23:48:11 CEST 2010


I just starting programming and am trying to learn some python (ver 2.6). I
am reading Python Programming: An Introduction to Computer Science by John
Zelle. In chapter ten, the first programming exercise asks the reader to
modify code from the chapter (below) . The code I added is highlighted.
However, when I did so I got this error: "TypeError: unbound method getY()
must be called with Projectile instance as first argument (got nothing
instead) " Can someone help me out with what I am doing wrong? Please be as
straitforward as you can. I am still struggling with classes

Thanks a lot


# cball4.py
> #   Simulation of the flight of a cannon ball (or other projectile)
> #   This version uses a  separate projectile module file
>
> from projectile import Projectile
>
> def getInputs():
>     a = input("Enter the launch angle (in degrees): ")
>     v = input("Enter the initial velocity (in meters/sec): ")
>     h = input("Enter the initial height (in meters): ")
>     t = input("Enter the time interval between position calculations: ")
>     return a,v,h,t
>
> def main():
>     angle, vel, h0, time = getInputs()
>     cball = Projectile(angle, vel, h0)
>     zenith = 0.0
>     while cball.getY() >= 0:
>         cball.update(time)
>         if Projectile.getY > zenith:
>             zenith = Projectile.getY()
>     print "\nDistance traveled: %0.1f meters." % (cball.getX())
>     print "The heighest the cannon ball reached was %0.1f meters." %
> (zenith)
>
> if __name__ == "__main__": main()
>
>
>

# projectile.py
>
> """projectile.py
> Provides a simple class for modeling the flight of projectiles."""
>
> from math import pi, sin, cos
>
> class Projectile:
>
>     """Simulates the flight of simple projectiles near the earth's
>     surface, ignoring wind resistance. Tracking is done in two
>     dimensions, height (y) and distance (x)."""
>
>     def __init__(self, angle, velocity, height):
>         """Create a projectile with given launch angle, initial
>         velocity and height."""
>         self.xpos = 0.0
>         self.ypos = height
>         theta = pi * angle / 180.0
>         self.xvel = velocity * cos(theta)
>         self.yvel = velocity * sin(theta)
>
>     def update(self, time):
>         """Update the state of this projectile to move it time seconds
>         farther into its flight"""
>         self.xpos = self.xpos + time * self.xvel
>         yvel1 = self.yvel - 9.8 * time
>         self.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0
>         self.yvel = yvel1
>
>     def getY(self):
>         "Returns the y position (height) of this projectile."
>         return self.ypos
>
>     def getX(self):
>         "Returns the x position (distance) of this projectile."
>         return self.xpos
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100825/efab7452/attachment.html>


More information about the Tutor mailing list