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
Modify your last GeoPoint program and add exception handling.
Wrap the code inside the main do another? loop of your last program with a try block.
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 initself:
#Set the default values to
self.lat
self.lon
self.description
#Set Point for self, lat, and lon
def setPointself lat, lon:
self.lat lat
self.lon lon
def setDescriptionself description:
self.description description
#Get Point for self.lat, self,lon
def getPointself tuple:
return selflat, self.lon
#Get Description for self return self description
def getDescriptionself str:
return self.description
#Method to calculate the distance of self, lat and lon with a tuple
def distanceself lat, lon tuple:
#Get the positive value with the absolute value function
return absselflat lat absselflon lon
#Main
def main:
#instantiate two points with point and point
point Geopoint
point Geopoint
#Set the points and descriptions
pointsetPoint
pointsetDescriptionpoint
pointsetPoint
pointsetDescriptionpoint
#while true loop
while True:
#Ask for the latitude and longitude
lat intinputEnter lat:
lon intinputEnter lon:
#Find the Distance for point and point
distanceToOne pointdistancelat lon
distanceToTwo pointdistancelat lon
#If distance to one is less than distance to two
if distanceToOne distanceToTwo:
printfYou are closest to pointgetDescription which is located at pointgetPoint
else: # if distance to two is less than distance to one
printfYou are closest to pointgetDescription which is located at pointgetPoint
#loop again yn
doAnother inputDo another yn
if doAnother y: #another cool way I learned to shorten the asking 'yes or no if statement' using IS NOT EQUAL TO:
printSee you again soon!
break
if namemain:
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 fSomething went wrong: e
doAnother Do another yn
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
