[Tutor] Trouble with exercise regarding classes

Denis Gomes denisg640 at gmail.com
Thu Aug 26 05:09:39 CEST 2010


Andrew,

     For starters you have some errors in the way you are trying to access
methods from within a class instances.  For example in your code in line 7
and 8,

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:          # This should be -- if
cball.getY()>zenith:
            zenith = Projectile.getY()       # This should be -- zenith =
cball.getY()
    print "\nDistance traveled: %0.1f meters." % (cball.getX())
    print "The heighest the cannon ball reached was %0.1f meters." %
(zenith)

You access the method of an instance using the instance name that you
created on line 3, not the class name.

Secondly, you have to change the time variable so that the value of
cball.getY() changes or else nothing will happen.  Assuming that the time
you are entering is a delta_t value, you can create another variable say
actual_time which starts at 0 and add delta_t to it at the end of the while
loop each time through.

actual_time=actual_time+delta_t

This will update your Y position because you will be calling
cball.update(actual_time).  You will converge to a solution.

Good luck,

Denis



On Wed, Aug 25, 2010 at 9:56 PM, Andrew Martin <amartin7211 at gmail.com>wrote:

> All I want to do is add a line that displays that maximum height the
> cannonball reaches. I created a variable zenith to store the highest y
> value. I then wanted to compare the current y value of the cannonball to
> zenith while the cannonballs y value is greater than zero. If the
> cannonballs current y value is greater than zenith, I want to have the
> current value replace zenith. Finally, once the cannonball has reaches y =
> 0, I wanted the program to write out the value for zenith.
>
> I want to compare zenith, a floating point number, with the current y
> value? I thought the current y value could be retrieved by Projectile.getY.
> And how do I call the getY using the instance?
>
>
>
>
> On Wed, Aug 25, 2010 at 7:24 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>
>>
>> "Andrew Martin" <amartin7211 at gmail.com> wrote
>>
>>
>>  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) "
>>>
>>
>>  def main():
>>>>    angle, vel, h0, time = getInputs()
>>>>    cball = Projectile(angle, vel, h0)
>>>>
>>>
>> cball is a Projectile instance
>>
>>
>>     zenith = 0.0
>>>>    while cball.getY() >= 0:
>>>>
>>>
>> So this is fine
>>
>>
>>         cball.update(time)
>>>>
>>>
>>
>>         if Projectile.getY > zenith:
>>>>            zenith = Projectile.getY()
>>>>
>>>
>> But what are you doing here?
>> You are trying to compare the getY method of the class with a floating
>> point number?
>> Then you call getY using the class rather than the instance?
>> I'm confused - and so is Python...
>>
>>
>> --
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100825/9e1050cc/attachment.html>


More information about the Tutor mailing list