Re: [Tutor] Trouble with exercise regarding classes

2010-08-28 Thread Andrew Martin
Ok I think I got it. Thanks everybody. And sorry for the late reply. My
classes have just started so learned python unfortunately must be bumped
down on the priority list

On Thu, Aug 26, 2010 at 4:32 AM, Alan Gauld wrote:

>
> "Andrew Martin"  wrote
>
>  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.
>>
>
> Projectile.getY is a reference to the getY method of the Projectile class.
> (See the separate thread on function objects for more on this topic)
>
> You want to execute the method so you need parens on the end.
>
> But, you also want to execute it for the cball instance.
> You have already done this earlier in your code, here:
>
>while cball.getY() >= 0:
>

> So you just need to make the if test compatible with that:
>
>
> if Projectile.getY > zenith:
>>>
>>
> becomes
>
>   if cball.getY() > zenith:
>
> And similarly for the assignment
>
>zenith = Projectile.getY()
>
 becomes
>   zenith = cball.getY()
>
>
> As an aside you can do this using Projectile, but its bad practice:
>
> Projectile.getY(cball)
>
> This explicitly provides the self argument instead of Python doing
> it for you when you use the instance. We can use this technique when
> calling inherited methods inside a class method definition. Anywhere
> else its best to use the instance to call a method.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble with exercise regarding classes

2010-08-26 Thread Alan Gauld


"Andrew Martin"  wrote

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.


Projectile.getY is a reference to the getY method of the Projectile 
class.

(See the separate thread on function objects for more on this topic)

You want to execute the method so you need parens on the end.

But, you also want to execute it for the cball instance.
You have already done this earlier in your code, here:


   while cball.getY() >= 0:


So you just need to make the if test compatible with that:



if Projectile.getY > zenith:


becomes

   if cball.getY() > zenith:

And similarly for the assignment


   zenith = Projectile.getY()

becomes
   zenith = cball.getY()


As an aside you can do this using Projectile, but its bad practice:

Projectile.getY(cball)

This explicitly provides the self argument instead of Python doing
it for you when you use the instance. We can use this technique when
calling inherited methods inside a class method definition. Anywhere
else its best to use the instance to call a method.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble with exercise regarding classes

2010-08-25 Thread Denis Gomes
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 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 wrote:
>
>>
>> "Andrew Martin"  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@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble with exercise regarding classes

2010-08-25 Thread Nitin Das
There are 2 ways to get the ypos value one is cball.ypos and another is
call.getY() both will give u the current ypos.

--nitin

On Thu, Aug 26, 2010 at 7:26 AM, Andrew Martin 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 wrote:
>
>>
>> "Andrew Martin"  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@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble with exercise regarding classes

2010-08-25 Thread Alex Hall
On 8/25/10, Andrew Martin  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
It can, but you need parentheses after the function call.
> do I call the getY using the instance?
I think the problem may be where you say
ball.getY
instead of
ball.getY()
When you hear "instance", do not panic. An instance is just a variable
of type class. For example, "ball" is an instance of the Projectile
class. As an example, if I had a "pet" class, I might make a "dog"
variable of type pet:
dog=Pet()
After I have my dog set up, since it is an instance of the Pet class,
it has all the methods available in the Pet class. I might say
dog.speak()
which would just look at my Pet class for a "speak" method, and call
it. In the same way, you have a "ball" variable which is  a
Projectile, so you have all the methods and variables from the
Projectile class available in the "ball" variable. Note that, if a
variable is of type class, it is also called an object, so I could
have as easily called the "ball" a Projectile object.
>
>
>
> On Wed, Aug 25, 2010 at 7:24 PM, Alan Gauld
> wrote:
>
>>
>> "Andrew Martin"  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@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble with exercise regarding classes

2010-08-25 Thread Andrew Martin
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 wrote:

>
> "Andrew Martin"  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@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble with exercise regarding classes

2010-08-25 Thread Alan Gauld


"Andrew Martin"  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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Trouble with exercise regarding classes

2010-08-25 Thread Andrew Martin
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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor