Question: How can I modify my code and wrap the code inside the main do another? loop with a try block? Language: Python 1 . Modify

How can I modify my code and wrap the code inside the main do another? loop with a try block?
Language: Python
1. Modify your last GeoPoint program and add exception handling.
2. Wrap the code inside the main do another? loop of your last program with a try block.
3. Add three except blocks.
a. The first will catch a TypeError exception and display Wrong type of input!
b. The second will catch an exception of your choice 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.
My code:
class Geopoint:
#Define the init method
def __init__(self):
#Set the default values to 0
self.lat =0
self.lon =0
self.description =''
#Set Point for self, lat, and lon
def setPoint(self, lat, lon):
self.lat = lat
self.lon = lon
def setDescription(self, description):
self.description = description
#Get Point for self.lat, self,lon
def getPoint(self)-> tuple:
return (self.lat, self.lon)
#Get Description for self | return self description
def getDescription(self)-> str:
return self.description
#Method to calculate the distance of self, lat and lon with a tuple
def distance(self, lat, lon)-> tuple:
#Get the positive value with the absolute value function
return (abs(self.lat - lat), abs(self.lon - lon))
#Main
def main():
#instantiate two points with point1 and point2
point1= Geopoint()
point2= Geopoint()
#Set the points and descriptions (2,3),(5,2)
point1.setPoint(2,3)
point1.setDescription('point 1')
point2.setPoint(5,2)
point2.setDescription('point 2')
#while true loop
while True:
#Ask for the latitude and longitude
lat = int(input('Enter lat: '))
lon = int(input('Enter lon: '))
#Find the Distance for point1 and point2
distanceToOne = point1.distance(lat, lon)
distanceToTwo = point2.distance(lat, lon)
#If distance to one is less than distance to two
if distanceToOne < distanceToTwo:
print(f'You are closest to {point1.getDescription()} which is located at {point1.getPoint()}')
else: # if distance to two is less than distance to one
print(f'You are closest to {point2.getDescription()} which is located at {point2.getPoint()}')
#loop again (y/n)?
doAnother = input('Do another (y/n)?')
if doAnother !='y': #<--another cool way I learned to shorten the asking 'yes or no if statement' using IS NOT EQUAL TO: !=
print('See you again soon!')
break
if __name__=='__main__':
main()
What I need to implement:
while doAnother =='y':
try:
'''your code here'''
except TypeError :
print ("Wrong type of input!")
except '''exception you picked''' :
print ('your custom message') #<-- EDIT
except Exception as e:
print (f"Something went wrong: {e}"),
doAnother =('Do another (y/n)?')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!