简体   繁体   中英

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. 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.
  • 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.
  • I understand that when I call the function for 'inCircle' that I need to use the first parameter, '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. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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