嗨我是python的新手,并在一个小项目上工作:
我想写一个程序来提供直接投射到空中的球高度的信息。程序应该要求输入初始高度,h英尺和初始速度,即每秒v英尺。 t秒后球的高度为h + vt - 16t2英尺。该程序应执行以下两个计算:
(a)确定球的最大高度。注意:球在v / 32秒后将达到最大高度。 (b)大致确定球何时会撞到地面。提示:每隔0.1秒计算一次高度,并确定高度何时不再为正数。应使用名为getInput的函数来获取h和v的值,该函数应调用名为isValid的函数以确保输入值为正数。任务(a)和(b)中的每一项都应由功能执行
def getInput():
h = int(input("Enter the initial height of the ball: "))
v = int(input("Enter the initial velocity of the ball: "))
isValid(h,v)
def isValid(h,v):
if (h<= 0):
print("Please enter positive values")
elif(v<= 0):
print("Please enter positive values")
else:
height = maxHeight(h,v)
print("The maximum height of the ball is", height, "feet.")
groundTime = ballTime(h,v)
print("The ball will hit the ground after approximately", groundTime, "seconds.")
def maxHeight(h,v):
t = (v/32)
maxH = (h + (v*t) - (16*t*t))
return maxH
def ballTime(h,v):
t = 0.1
while(True):
ballHeight = (h + (v*t) - (16*t*t))
if (ballHeight <= 0):
break
else:
t += 0.1
return t
getInput()
> Enter the initial height of the ball: 5
> Enter the initial velocity of the ball: 34
-The maximum height of the ball is 23.06 feet.
-The ball will hit the ground after approximately 2.27 seconds.
看起来你正在使用IPython?你可能不得不摆脱elif和if body之间的空间。它似乎完成了对那里的行的解释然后,如果你要逐个输入这些行,那么Python解释器将如何停止解释。
def getInput():
h = int(input("Enter the initial height of the ball: "))
v = int(input("Enter the initial velocity of the ball: "))
isValid(h,v)
def isValid(h,v):
if (h<= 0):
print("Please enter positive values")
elif(v<= 0):
print("Please enter positive values")
else:
height = maxHeight(h,v)
print("The maximum height of the ball is", height, "feet.")
groundTime = ballTime(h,v)
print("The ball will hit the ground after approximately", groundTime, "seconds.")
def maxHeight(h,v):
t = (v/32)
maxH = (h + (v*t) - (16*t*t))
return maxH
def ballTime(h,v):
t = 0.1
while(True):
ballHeight = (h + (v*t) - (16*t*t))
if (ballHeight <= 0):
break
else:
t += 0.1
return t
getInput()