# 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)
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.