我试图做一个程序,将确定一个球的最大高度扔到空中使用独立的函数,每个将接受输入,确定它是否有效,然后计算高度。 我得到了前两个工作,但maxHeight函数将返回一个NameError,说明它或其中一个参数未定义。 我知道输入函数中的值没有被传递。 提前感谢你的帮助。
def isValid(h, v):
if h and v > -1:
pass
else:
print("Please enter a nonnegative number")
getInput()
def getInput():
h = int(input("Enter initial height of the ball:"))
v = int(input("Enter initial velocity of the ball:"))
isValid(h, v)
return (h, v)
def maxHeight(h, v):
height = h
velocity = v
maximum = height + (velocity / 32)
return maximum
print(getInput())
print(maxHeight(h, v))
输入和回溯示例
Enter initial height of the ball:5
Enter initial velocity of the ball:5
(5, 5)
Traceback (most recent call last):
File ".\test.py", line 23, in <module>
print(maxHeight(h,v))
NameError: name 'h' is not defined
在print(maxHeight(h,v))
行,您尚未定义h
或v
。 GetInput
中的h
和v
仅存在于该函数中。 您应该将它们从函数中取出到全局(模块)范围:
h, v= getInput()
print(h,v)
print(maxHeight(h,v))