简体   繁体   English

在另一个函数中使用一个函数中的变量然后调用它

[英]Using a Variable from One Function Inside Another Function then Calling It

# Write a function inCircle that takes a point and a radius as a parameter. 
# The function should return True if the point is inside the circle and False otherwise.

import math


# Function for distance equation
def pointCalc(x1, y1, x2, y2):
    distance = math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))


# Function for whether or not coordinate points are in the in/on the circle
def inCircle(point, radius):
    """Function that returns whether point is inside a circle"""
    if pointCalc.distance >= radius:
        print(f"Point ({pointCalc.x1},{pointCalc.x2}) is not in circle with radius {radius}")
    else:
        print(f"Point ({pointCalc.x1},{pointCalc.x2}) is in circle with radius {radius}")


# Prompt user for inputs on coordinate points, center, and radius
print("Enter coordinate points:")
print("---------------------------------------------")
point_x = float(input("Enter a x-coordinate point: "))
point_y = float(input("Enter a y-coordinate point: "))

print("\nEnter coordinate points for the center:")
print("---------------------------------------------")
center_x = float(input("Enter a x-coordinate point for the center: "))
center_y = float(input("Enter a y-coordinate point for the center: "))

r = float(input("\nEnter radius for the circle: "))


# Call functions
pointCalc(point_x, point_y, center_x, center_y)
inCircle(radius=r)
  • Essentially, I went more extreme than asked because I just like complicating stuff, it makes me learn better.从本质上讲,我比要求的更极端,因为我只是喜欢把事情复杂化,这让我学得更好。
  • But I want to prompt the user to enter coordinate points (x, y) and then have them enter coordinate points for the center of a circle.但我想提示用户输入坐标点 (x, y),然后让他们输入圆心的坐标点。 I then want them to define a radius.然后我希望他们定义一个半径。
  • After all of that, I want to run the coordinate points and the coordinate points for the center through the 'pointCalc' function that calculates the distance.毕竟,我想通过计算距离的“pointCalc”函数运行坐标点和中心坐标点。
  • I then want to use the 'distance' variable located in the 'pointCalc' function and insert it in the if/else statements in the 'inCircle' function to see if the 'radius' is greater than, less than, equal to, etc... and print out the messages in the if/else statements.然后,我想使用位于“pointCalc”函数中的“距离”变量,并将其插入到“inCircle”函数的 if/else 语句中,以查看“半径”是否大于、小于、等于等...并打印出 if/else 语句中的消息。
  • I understand that when I call the function for 'inCircle' that I need to use the first parameter, 'point'.我知道当我调用“inCircle”的函数时,我需要使用第一个参数“point”。 I just cannot figure out how to bypass it, it is not working when I try to define it when called.我只是不知道如何绕过它,当我尝试在调用时定义它时它不起作用。

So in essence you want to pass a variable calculated in a function to another function?所以本质上你想把一个函数中计算出的变量传递给另一个函数?

Then the right way to do it (in most of the programming languages) is to return the value from the function.那么正确的做法(在大多数编程语言中)是从函数return值。 You aren't using return in your functions, and you can't access the variables inside of another function through a syntax like func.variable - a function isn't an object (kind of) and the variables it declares are destroyed once the function terminates.你没有在你的函数中使用return ,你不能通过像func.variable这样的语法访问另一个函数内部的变量 - 一个函数不是一个对象(一种),一旦它声明的变量就会被销毁函数终止。

So in your case:所以在你的情况下:

# Function for distance equation
def pointCalc(x1, y1, x2, y2):
    return math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))


# Function for whether or not coordinate points are in the in/on the circle
def inCircle(distance, radius):
    """Function that returns whether point is inside a circle"""
    if distance >= radius:
        print(f"Point ({pointCalc.x1},{pointCalc.x2}) is not in circle with radius {radius}")
    else:
        print(f"Point ({pointCalc.x1},{pointCalc.x2}) is in circle with radius {radius}")

# ...

# Call functions
d = pointCalc(point_x, point_y, center_x, center_y)
inCircle(distance=d, radius=r)

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

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