Python 函数未将值传递给其他函数
我需要Calc_Velocity_Factor将Velocity_因子传递给Calc_函数,同时将Velocity_因子返回给Main() 运行时,所有操作都正常工作,除了我得到一个“NameError:name”“Velocity\u Factor”“未定义” Velocity_Factor是返回值和参数,我不知道是否需要更改名称,然后将其作为参数输入Python 函数未将值传递给其他函数,python,function,parameters,parameter-passing,return-value,Python,Function,Parameters,Parameter Passing,Return Value,我需要Calc_Velocity_Factor将Velocity_因子传递给Calc_函数,同时将Velocity_因子返回给Main() 运行时,所有操作都正常工作,除了我得到一个“NameError:name”“Velocity\u Factor”“未定义” Velocity_Factor是返回值和参数,我不知道是否需要更改名称,然后将其作为参数输入 def main(): 输入数学 ang=输入(“\n输入起飞角度:\n”); 速度=输入(“输入速度:\n”); 秒=输入(“输入起飞秒数:
def main():
输入数学
ang=输入(“\n输入起飞角度:\n”);
速度=输入(“输入速度:\n”);
秒=输入(“输入起飞秒数:\n”);
#计算
角度=浮动(ang)
速度=浮动(速度)
秒=浮动(秒)
弧度=角度*3.1415926/180
返回的距离=计算距离(速度、弧度、秒)
打印(“\n您的距离为:”,返回\u距离)
返回的速度系数=计算速度系数(速度、弧度、秒)
打印(“\n您的速度因子为:”,返回速度因子)
返回的高度=计算高度(秒,速度系数)
打印(“\n您的高度为:”,返回\u高度)
def Calc_距离(速度、弧度、秒):
输入数学
距离=速度*数学cos(弧度)*秒
返回距离
def Calc_速度系数(速度、弧度、秒):
输入数学
速度系数=速度*数学正弦(弧度)*秒
返回速度系数
def校准高度(秒,速度系数):
输入数学
高度=-0.5*9.8*秒**2+速度系数
返回高度
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
只需更改此选项即可
returned_Height = Calc_Height(Seconds, Velocity_Factor)
print("\nYour Height is:", returned_Height)
对此
returned_Height = Calc_Height(Seconds, returned_Velocity_Factor)
print("\nYour Height is:", returned_Height)
这个问题应该得到解决
另一个技巧:确保始终在模块顶部定义导入,而不是在调用的每个函数中定义导入
def main():
import math
ang = input("\nEnter your angle of takeoff:\n");
velo = input("Enter your velocity:\n");
secs = input("Enter your seconds of takeoff:\n");
# Calculations
Angle = float(ang)
Velocity = float(velo)
Seconds = float(secs)
Radians = Angle * 3.1415926 / 180
returned_Distance = Calc_Distance(Velocity, Radians, Seconds)
print("\nYour Distance is:", returned_Distance)
returned_Velocity_Factor = Calc_VelocityFactor(Velocity, Radians, Seconds)
print("\nYour Velocity Factor is:", returned_Velocity_Factor)
returned_Height = Calc_Height(Seconds, returned_Velocity_Factor)
print("\nYour Height is:", returned_Height)
def Calc_Distance(Velocity, Radians, Seconds):
import math
distance = Velocity * math.cos(Radians) * Seconds
return distance
def Calc_VelocityFactor(Velocity, Radians, Seconds):
import math
Velocity_Factor = Velocity * math.sin(Radians) * Seconds
return Velocity_Factor
def Calc_Height(Seconds, Velocity_Factor):
import math
Height = -0.5 * 9.8 * Seconds ** 2 + Velocity_Factor
return Height
if __name__ == '__main__':
main()
发生此错误的原因是,Calc\u VelocityFactor
的返回值被分配给第20行中的returned\u Velocity\u Factor
,但您在第21行中传递的是不存在的Velocity\u Factor
。将速度系数
替换为返回的速度系数
返回高度=计算高度(秒,返回速度系数)