python问题射弹.py

2024-06-16 09:35:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使此代码正常工作,但不断出现错误:

angle, vel, h0, time = getInputs()

NameError: name 'getInputs' is not defined" is not defined.

有人能帮我弄清楚为什么会这样吗?你知道吗

from math import sin, cos, radians

class Projectile:

     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)
          self.maxypos = max(self.maxypos, self.ypos)
     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 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()))

if __name__ == "__main__":
main()

Tags: theselfinputtimedefevalenterheight
1条回答
网友
1楼 · 发布于 2024-06-16 09:35:32

getInputs的缩进处于关闭状态(它位于类中):

(注意对用户输入使用eval是一个非常糟糕的主意)

from math import sin, cos, radians

class Projectile:

    def __init__(self, angle, velocity, height):
        """Create a projectile with given launch angle, initial
        velocity and height."""
        self.xpos = 0.0
        self.maxypos = self.ypos = height   # defining self.maxypos
        theta = radians(angle)
        self.xvel = velocity * cos(theta)
        self.yvel = velocity * sin(theta)
        self.maxypos = max(self.maxypos, self.ypos)

    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 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()))

if __name__ == "__main__":
    main()

修正了@PM2Ring指出的self.maxypos声明

相关问题 更多 >