您好,我是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.
答案 0 :(得分:1)
看起来您正在使用IPython?您可能必须摆脱之前的elif和if主体之间的空间。似乎已经完成了对行的解释,然后,如果您要一一输入这些行,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()