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<br>
<br>Thanks a lot<br><br><br><blockquote style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;" class="gmail_quote"># cball4.py<br># Simulation of the flight of a cannon ball (or other projectile)<br>
# This version uses a separate projectile module file<br><br>from projectile import Projectile<br><br>def getInputs():<br> a = input("Enter the launch angle (in degrees): ")<br> v = input("Enter the initial velocity (in meters/sec): ")<br>
h = input("Enter the initial height (in meters): ")<br> t = input("Enter the time interval between position calculations: ")<br> return a,v,h,t<br><br>def main():<br> angle, vel, h0, time = getInputs()<br>
cball = Projectile(angle, vel, h0)<br> zenith = 0.0<br> while cball.getY() >= 0:<br> cball.update(time)<br> <span style="background-color: rgb(255, 255, 51);">if Projectile.getY > zenith:</span><br style="background-color: rgb(255, 255, 51);">
<span style="background-color: rgb(255, 255, 51);"> zenith = Projectile.getY()</span><br> print "\nDistance traveled: %0.1f meters." % (cball.getX())<br> <span style="background-color: rgb(255, 255, 51);">print "The heighest the cannon ball reached was %0.1f meters." % (zenith)</span><br>
<br>if __name__ == "__main__": main()<br><br><br></blockquote><div><br><br><blockquote style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;" class="gmail_quote"># projectile.py<br>
<br>"""projectile.py<br>Provides a simple class for modeling the flight of projectiles."""<br> <br>from math import pi, sin, cos<br><br>class Projectile:<br><br> """Simulates the flight of simple projectiles near the earth's<br>
surface, ignoring wind resistance. Tracking is done in two<br> dimensions, height (y) and distance (x)."""<br><br> def __init__(self, angle, velocity, height):<br> """Create a projectile with given launch angle, initial<br>
velocity and height."""<br> self.xpos = 0.0<br> self.ypos = height<br> theta = pi * angle / 180.0<br> self.xvel = velocity * cos(theta)<br> self.yvel = velocity * sin(theta)<br>
<br> def update(self, time):<br> """Update the state of this projectile to move it time seconds<br> farther into its flight"""<br> self.xpos = self.xpos + time * self.xvel<br>
yvel1 = self.yvel - 9.8 * time<br> self.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0<br> self.yvel = yvel1<br><br> def getY(self):<br> "Returns the y position (height) of this projectile."<br>
return self.ypos<br><br> def getX(self):<br> "Returns the x position (distance) of this projectile."<br> return self.xpos<br></blockquote></div><br><input id="gwProxy" type="hidden"><input onclick="jsCall();" id="jsProxy" type="hidden"><div id="refHTML">
</div>