Question
PYTHON EXCEPTION 2. 5 pts. Add comments as appropriate. Be sure that your program output is neatly presented to the user. Add documentation comments to
PYTHON EXCEPTION
2. 5 pts. Add comments as appropriate. Be sure that your program output is neatly presented to the user. Add documentation comments to your functions. 3. Modify your last GeoPoint program and add exception handling. 4. Wrap the code inside the main do another? loop of your last program with a try block. 5. Add three except blocks. a. The first will catch a TypeError exception and display Wrong data type! b. The second will catch an NameError and display a message that makes sense. c. The third will catch a generic Exception along with its object e and display the message Something went wrong: , e so that the error message e is displayed. Hints The code will look something like this (yours may vary): doAnother = 'y' while doAnother == 'y': try your code here except TypeError : print "Wrong data type!" except NameError: print your custom message except Exception,e: print "Something went wrong: ", e doAnother = input('Do another (y/n)? ')
Original program that needs to be modified:
from math import cos,asin,sqrt,pi
#defining the class GeoPoint
class GeoPoint:
#constructor for the class
def __init__(self):
#initializing the attributes
self.lat = ""
self.lon = ""
self.description = ""
#function that will set the values of self.lat,self.lon
def SetPoint(self,lat,lon):
self.lat = lat
self.lon = lon
#method that will return a tuple or list with self.lat, self.lon
def GetPoint(self): return (self.lat,self.lon)
#method that will figure out the distance between the objects
#self.lat, self.lon and lat, lon user parameters passed in
def Distance(self,lat,lon): p = pi/180 a = 0.5 - cos((lat-self.lat)*p)/2 + cos(self.lat*p) * cos(lat*p) * (1-cos((lon-self.lon)*p))/2
return 12742 * asin(sqrt(a))
#method that will set the objects self.description attribute
def SetDescription(self,description): self.description = description
#method that will return the objects self.description attribute
def GetDescription(self): return self.description
#driver code
#creating objects for the class GeoPoint
ABQ = GeoPoint()
DENVER = GeoPoint()
#seeting the points
ABQ.SetPoint(35.106766, -106.629181)
DENVER.SetPoint(39.742043, -104.991531)
#setting the description
ABQ.SetDescription("Albuquerque")
DENVER.SetDescription("Denver")
#setting the loop
while True:
lat,lon = map(float,input("Enter the location points of lattitude and longitue : ").split(","))
#calculate distance
distanceToOne = ABQ.Distance(lat, lon)
distanceToTwo = DENVER.Distance(lat, lon)
if distanceToOne > distanceToTwo:
print(f"You are closest to {ABQ.GetDescription()} which is located at {ABQ.lat,ABQ.lon} ")
else: print(f"You are closest to {DENVER.GetDescription()} which is located at {DENVER.lat,DENVER.lon} ")
inn = input("Do another distance calculation (y/n)?")
if inn == "n":
break
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started