简体   繁体   English

你如何使用一个function的output作为另一个输入值?

[英]How do you use the output of one function, as an inputted value in another?

I am doing a coding project for school, where I have the following prompt:我正在为学校做一个编码项目,我有以下提示:

In a target practice exercise, a student holds a rifle 5 meters away from a target.在一次打靶练习中,一名学员手持步枪,距离目标5米远。 The target is tossed up at 5 meters/second.目标以 5 米/秒的速度抛起。 Write a function that takes in the angle (in degrees) at which the student is holding their rifle and provides the time in seconds after the target is released that the student should shoot.编写一个 function,其中包含学生手持步枪的角度(以度为单位),并提供目标释放后学生应该射击的时间(以秒为单位)。 Specify units in your input().在您的 input() 中指定单位。 (Assume the speed of the target is constant - ignore gravity). (假设目标的速度是恒定的——忽略重力)。 Make sure to include a caller after and a docstring in all of your functions.确保在所有函数中都包含一个调用者和一个文档字符串。 Use the math module.使用数学模块。

I have the following code:我有以下代码:

#Imports the math function
import math

#Prompts the user to input the angle of which the rifle is pointed in the air
angle = float(input("Enter the angle of which the rifle is pointed in the air (In degrees): "))

#Function to determine length of shot
def length_of_shot():
    #Multiplies the inputted angle (Converted to radians) with the adjancent side (5) to figure out opposite side (Height in the air)
    return math.tan(math.radians(angle)) * 5

#Function to determine time to shoot
def time_to_shoot(): 
    #Divide by 5 since the target is tossed in the air at 5 meters / second
    return lenth_of_shot / 5

#Calls the time_to_shoot function
time_to_shoot()

I am trying to figure out how to take the output value from the function "length_of_shot"我想弄清楚如何从 function“length_of_shot”中获取 output 值

def length_of_shot():
    return math.tan(math.radians(angle)) * 5

And use it in this equation in the function "time_to_shoot"并在 function "time_to_shoot" 的这个等式中使用它

def time_to_shoot(): 
    return length_of_shot / 5

You need to provide parameters to your functions, and use their return values.您需要为您的函数提供参数,并使用它们的返回值。 Also, you need to call length_of_shot to get its return value, as otherwise you'll be dividing the function object, which will result in an error.此外,您需要调用length_of_shot以获取其返回值,否则您将除以 function object,这将导致错误。

#Imports the math function
import math

#Prompts the user to input the angle of which the rifle is pointed in the air
angle = float(input("Enter the angle of which the rifle is pointed in the air (In degrees): "))

#Function to determine length of shot
def length_of_shot(angle):
    #Multiplies the inputted angle (Converted to radians) with the adjancent side (5) to figure out opposite side (Height in the air)
    return math.tan(math.radians(angle)) * 5

#Function to determine time to shoot
def time_to_shoot(angle): 
    #Divide by 5 since the target is tossed in the air at 5 meters / second
    return length_of_shot(angle) / 5

#Calls the length_of_shot function
time_to_shoot(angle)

When the instructions say "Write a function that takes in the angle ", they mean that the angle should be passed in as a parameter.当指令说“Write a function that takes in the angle ”时,它们的意思是应该将角度作为参数传入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM