中国大学 MOOC 课程 《Python 语言程序设计》第七周课上练习
1、铅球运行轨迹
from math import pi,sin,cos,radians
def main():
angle,vel,h0,time = getInputs()
xpos,ypos=0,h0
xvel,yvel=getXYComponents(vel,angle)
while ypos>= 0:
xpos,ypos,yvel=updatePosition(time,xpos,ypos,xvel,yvel)
print("\nDistance traveled:{0:0.1f}meters".format(xpos))
def getInputs():
angle = eval(input("Enter the launch angle (in degrees):"))
vel = eval(input("Enter the initial velocity (in meters/sec):"))
h0 = eval(input("Enter the initial height (in meters):"))
time = eval(input("Enter the time interval: "))
return angle,vel,h0,time
def getXYComponents(vel,angle):
thera=radians(angle)
xvel=vel*cos(thera)
yvel=vel*sin(thera)
return xvel,yvel
def updatePosition(time,xpos,ypos,xvel,yvel):
xpos=xpos+time*xvel
yvell=yvel-9.8*time
ypos=ypos+(yvel+yvell)/2.0
yvel=yvell
return xpos,ypos,yvel
main()