获取错误:get_distance() missing 1 required positional argument: 'point' when the function is already defined with an positional argument called point.
这是课程:
from math import radians, sin, cos, acos, atan2, sqrt
class GeoPoint:
lat = ''
lon = ''
# Initialize
def __init__(self, lat=0, lon=0, des='TBD') -> None:
self.lat = lat
self.lon = lon
self.description = des
def set_point(self, point):
self.lat = point[0]
self.lon = point[1]
def get_point(self):
return (self.lon, self.lat)
# Properties
point = property(get_point, set_point)
def get_distance(self, point):
lat1 = radians(self.lat)
lon1 = radians(self.lon)
lat2 = radians(point.lat)
lon2 = radians(point.lon)
print("\nInput coordinates of two points:")
lat1 = radians(float(input("Starting latitude: ")))
lon1 = radians(float(input("Ending longitude: ")))
lat2 = radians(float(input("Second Starting latitude: ")))
lon2 = radians(float(input("Second Ending longitude: ")))
# Problem solving distance
dist = 6371.01 * acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon1 - lon2))
print("The distance is %.2fkm." % dist)
def get_description(self):
return (self.description)
def set_description(self,description):
self.description = description
这是脚本:
from GeoPoint import GeoPoint
def doanother():
'''Asks user if they want to do another and returns True or False'''
usr_response = input("Do another (y/n)? ")
another = usr_response.strip()[0].lower() == 'y'
return another
while True:
try:
dist = ()
geo = GeoPoint
point1 = GeoPoint(12.3456, -123.4567, 'Loc1')
point2 = GeoPoint()
point2.point = 23.4567, -213.456
point2.description = 'Loc2'
# get user coordinates
choice = (input('Enter the lat and lon of your location: '))
latlon = tuple(map(float, choice.split(', ')))
print("latlon:",latlon)
print('latlon[0]:',latlon[0])
print('latlon[1]:',latlon[1])
Loc = (input('Enter location: '))
#Get distance to point 1
point3 = GeoPoint(latlon[0], latlon[1], Loc)
# point3 = GeoPoint()
# point3.point = latlon
dist[1] = geo.get_distance(point3)
#Get distance to point 1
point3 = GeoPoint(latlon, Loc)
dist[2] = geo.get_distance(point3)
distance = min(dist)
# show result
print(f'The shortest distance is: {min(dist)} ')
except Exception as e:
print(f'Error: {e}')
if not doanother(): break
这完全令人费解。我检查以确保在使用它之前实例化了该类。我不知道为什么 Python 认为 get_distance(self, point) 需要一个额外的参数。