该程序提供了一个简单的类,用于建模射弹的飞行

时间:2017-11-06 22:47:40

标签: python

我正在尝试编写一个程序,该程序代表一个简单的类,用于对射弹的飞行进行建模,并返回最大高度和行进距离。以下是我到目前为止的情况:

th import sin, cos, radians

class Projectile:
    """Simulates the flight of  simple projectiles near the eath's surface, ignoring 
wind resisitance.  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 projectiles max height.
        self.th = self.yvel/9.8

    def update(self, time):
        """Update the stat of this projectile to move in 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
        self.maxpos = max(self.maxypos, self.ypos)

    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 getMaxHeight(self):
        """Returns the max height of the projectile."""

        return self.maxypos

    def getInputs():
        a = eval(input("Enter the launch angle (in degrees): "))
        v = eval(input("Enter the inital velocity (in meters/sec): "))
        h = eval(input("Enter the inital height (in meters): "))
        t = eval(input("Enter the time interval between position calculations:"))
        return a, v, h, t

def main():
    angle, vel, h0, time = getInputs() 
    elapsedTime = 0.0
    cball = Projectile(angle, vel, h0)
    cball.getInputs()
    while cball.getY() >= 0:
        cball.update(time)
        elapsedTime + time
    print("\nDistance traveled: {0:0.1f} meters.".format(cball.getX()),
          " in {0:0.1f} seconds.".format(elapsedTime))
    print("\nMaximum height traveled: {0:0.1f} meters.".format(cball.getMaxY()))

if __name__ == "__main__":
    main()

然而,我在main()中遗漏了一些东西,因为它一直告诉我“getInputs'没有定义,对于我的生活,我无法弄明白。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

当你的缩进得到纠正后,它会起作用;此时,你的getInputs()函数可能会以一种在你的类中定位它的方式缩进 第cball.getInputs()行是多余的 用户原始输入的eval不是一个好主意,可以执行任意代码。

通常,将i / o与业务逻辑混合起来并不是一个好主意。你的班级不应该窝藏i / o;将getInputs()作为一个函数更好。

from math import sin, cos, radians


class Projectile:
    """Simulates the flight of  simple projectiles near the eath's surface, ignoring
    wind resisitance.  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 projectiles max height.
        self.th = self.yvel/9.8
        self.maxypos = float('-inf')

    def update(self, time):
        """Update the stat of this projectile to move in 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
        self.maxypos = max(self.maxypos, self.ypos)

    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 max height of the projectile."""

        return self.maxypos

def getInputs():
#     a = float(input("Enter the launch angle (in degrees): "))
#     v = float(input("Enter the inital velocity (in meters/sec): "))
#     h = float(input("Enter the inital height (in meters): "))
#     t = float(input("Enter the time interval between position calculations:"))
#     return a, v, h, t
    return 1, 2, 3, 4

def main():
    angle, vel, h0, time = getInputs() 
    elapsedTime = 0.0
    cball = Projectile(angle, vel, h0)
    while cball.getY() >= 0:
        cball.update(time)
        elapsedTime + time
    print("\nDistance traveled: {0:0.1f} meters.".format(cball.getX()),
          " in {0:0.1f} seconds.".format(elapsedTime))
    print("\nMaximum height traveled: {0:0.1f} meters.".format(cball.getMaxY()))

if __name__ == "__main__":
    main()

答案 1 :(得分:0)

您在self中错过getInputs作为参数。它应该是

def getInputs(self)

然后您可以使用cball调用它。

修改:上述建议会使cball.getInputs()有效。

但正如评论中所提到的,另一种解决方案是将getInputs()从Projectile类中移出,在这种情况下,你不会通过自我'作为参数。