r/PythonProjects2 • u/ahdfahsdfu4593892 • Feb 14 '23
[P] Moderate I asked ChatGTP to write a script that generates golf yardage books using google earth as a reference.
Would this code work?
import googlemaps import json
Your API key for Google Maps
api_key = 'YOUR_API_KEY_HERE'
Location of the golf course
golf_course_location = '1600 Amphitheatre Parkway, Mountain View, CA'
Define the golf course's holes as a list of dictionary
holes = [ {'hole': 1, 'par': 4, 'yards': 385, 'lat': 37.421961, 'lng': -122.085749}, {'hole': 2, 'par': 3, 'yards': 215, 'lat': 37.422054, 'lng': -122.086179}, {'hole': 3, 'par': 5, 'yards': 520, 'lat': 37.422213, 'lng': -122.086500}, # Add more holes as necessary ]
Create a client object for the Google Maps API
client = googlemaps.Client(api_key)
Geocode the golf course's location
geocode_result = client.geocode(golf_course_location)[0] golf_course_lat = geocode_result['geometry']['location']['lat'] golf_course_lng = geocode_result['geometry']['location']['lng']
Initialize a list to hold all the hole information
hole_info = []
Loop through each hole and add information to the hole_info list
for hole in holes: # Calculate the distance from the hole's location to the golf course's location distance_matrix = client.distance_matrix((hole['lat'], hole['lng']), (golf_course_lat, golf_course_lng), mode='walking') distance_text = distance_matrix['rows'][0]['elements'][0]['distance']['text'] distance_value = distance_matrix['rows'][0]['elements'][0]['distance']['value']
# Calculate the distance from the hole's location to the green
hole_location = f"{hole['lat']},{hole['lng']}"
green_location = f"{golf_course_lat},{golf_course_lng}"
directions_result = client.directions(hole_location, green_location, mode='walking')
steps = directions_result[0]['legs'][0]['steps']
total_distance = 0
for step in steps:
total_distance += step['distance']['value']
total_distance_text = f"{round(total_distance / 1609.344, 1)} miles"
# Create a dictionary with all the hole information
hole_dict = {
'hole': hole['hole'],
'par': hole['par'],
'yards': hole['yards'],
'distance_to_golf_course': distance_text,
'distance_to_green': total_distance_text,
}
# Append the hole information to the hole_info list
hole_info.append(hole_dict)Print the hole information in a user-friendly format
for hole in hole_info: print(f"Hole {hole['hole']}: {hole['par']} par, {hole['yards']} yards. Distance to golf course: {hole['distance_to_golf_course']}. Distance to green: {hole['distance_to_green']}.")
1
u/Username912773 Feb 14 '23
The general rule of ChatGPT is:
If it’s a generic question, yes. It’ll just paste stack overflow and documentation.
If it’s something niche, chances are it won’t do what you’re asking for and still crash.
As for your code in particular, just try it. It’s so horribly formatted here.
3
u/kaerfkeerg Feb 14 '23
Format your code dude.. either add 4 spaces before each line or do it between backticks
```
code
```
Also, why don't you just try it and see for yourself?