Python 此方法工作流中缺少什么?
我正在学习如何使用书本上的方法,在这个练习中,我试图找到投射物的最大高度。我很确定我把这个方程放在了Sproject.py中,但是每次我执行练习1.py,我从Python 此方法工作流中缺少什么?,python,Python,我正在学习如何使用书本上的方法,在这个练习中,我试图找到投射物的最大高度。我很确定我把这个方程放在了Sproject.py中,但是每次我执行练习1.py,我从cball.getMaxY()得到的结果总是“-0.0米。”我可能错过了哪些简单的东西 # projectile.py """projectile.py Provides a simple class for modeling the flight of projectiles.""" from math import sin, co
cball.getMaxY()
得到的结果总是“-0.0米。”
我可能错过了哪些简单的东西
# projectile.py
"""projectile.py
Provides a simple class for modeling the
flight of projectiles."""
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)
#Find time to reach projectile's maximum height
self.th = self.yvel/9.8
def update(self, time):
"""Update the state of this projectile to move it time seconds
farther into its flight"""
#Find max height
self.maxypos = self.yvel - (9.8 * self.th)
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 getMaxY(self):
"Returns the maximum height of the projectile."
return self.maxypos
这句话没有多大意义:
self.maxypos = self.yvel - (9.8 * self.th)
保持简单;尝试以下方法:
self.maxypos = max(self.maxypos, self.ypos)
更新self.ypos
后。您还需要在中初始化self.maxypos=self.ypos
你不需要所有这些琐碎的获取者;只需访问属性() 这里的代码使用这个抱歉,刚刚添加了它。调试提示。添加一个print语句到update()并观察maxypos的值如何变化。谢谢David。调试之后,我发现我并没有正确地处理这个等式:)
self.maxypos = max(self.maxypos, self.ypos)