So for this program im trying to print out the max height the projectile reaches and when i run the str on the object it dosent change the value to the string and just gives a garbage result. the program als o prints some other vaulues but my problem is with the max height portion

``````from math import sin, cos, radians
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 = radians(angle)
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
def getInputs():
a = eval(input("Enter the launch angle (in degrees): "))
v = eval(input("Enter the initial velocity (in meters/sec): "))
h = eval(input("Enter the initial height (in meters): "))
t = eval(input("Enter the time interval between position calculations: "))
return a,v,h,t
def maxHeight():
time=self.yvel/9.8
self.maxheight=self.ypos+time*(self.yvel/2.0)
return self.maxheight
def __str__():
return str(maxHeight)
def main():
angle, vel, h0, time = getInputs()
cball = Projectile(angle, vel, h0)
while cball.getY() >= 0:
cball.update(time)
print("\nDistance traveled: {0:0.1f} meters.".format(cball.getX()))
print("\nMaximun height:" + str(maxHeight) + " meters")
if __name__ == "__main__":
main()``````

this returns
Distance traveled: 232.5 meters.

Maximun height:<function maxHeight at 0x05A2E9C0> meters

input
Enter the launch angle (in degrees): 60
Enter the initial velocity (in meters/sec): 50
Enter the initial height (in meters): 20
Enter the time interval between position calculations: 0.1

## Recommended Answers

I'm a Python beginner but line 36 looks to missing it's self. to the the variable.

Line 43 seems to have a similar issue so my view is maxHeight() is one object and self.maxHeight is a value.
So line 43 might be as follows once line 36 is fixed to …

## All 2 Replies

I'm a Python beginner but line 36 looks to missing it's self. to the the variable.

Line 43 seems to have a similar issue so my view is maxHeight() is one object and self.maxHeight is a value.
So line 43 might be as follows once line 36 is fixed to be:
`print("\nMaximun height:" + Projectile() + " meters")`

Why?

print will automatically call str() on the object and as such use the type’s str method to convert it to a string.

Sorry if I mislead you but I'm just a Python beginner too.

First, your maxHeight needs to be a member of the class, so move it up. And it needs self as a parameter, like:

``````    def getX(self):
"Returns the x position (distance) of this projectile."
return self.xpos
def maxHeight(self):
time=self.yvel/9.8
self.maxheight=self.ypos+time*(self.yvel/2.0)
return self.maxheight``````

Then your problem with str( ) needs two fixes. As is, you asking to convert the function to a string, not its return value. You also need to append it class object. Like:

``    print("\nMaximun height:" + str(cball.maxHeight() ) + " meters")``

I'm not sure the purpose of your `__str__()` function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.