python language, practice with classes and objects Preliminaries Throughout this
ID: 3705535 • Letter: P
Question
python language, practice with classes and objects
Preliminaries Throughout this lab you will be working with the class given below, which defines the characteristics of a package being shipped in the mail: class Package: def --init_ (self, self.sender-sender self.recipient - recipient self.cost-cost self.distance-distance sender, recipient, cost-O, distance-0): sender and recipient are strings that specify the cities where the sender and recipient of the package live, This class definition is available in the file package.py. DO NOT CHANGE THE CONTENTS OF THAT FILEExplanation / Answer
import math
import package
def package_tracking(package_info,locations,cost_schedule):
out_list=[]
#Loop for each sender,receiver combination in list package info.
for package in package_info:
sender=package[0]
recipient=package[1]
x1=locations[sender][0]
y1=locations[sender][1]
x2=locations[recipient][0]
y2=locations[recipient][1]
#implementing the distance calculation formula
distance=math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
if(distance<100):
cost=cost_schedule[0]
elif(distance>=100 and distance<300):
cost=cost_schedule[1]
elif(distance>=300 and distance<500):
cost=cost_schedule[2]
else:
cost=cost_schedule[2]
#creating the object
temp_package=Package(sender,recipient,cost,distance)
#Appending the object created for this loop in a list for output
out_list.append(temp_package)
return out_list
#Function Call
output=package_tracking([('Appleby','Satbury'),('Northpass','Berkton'),('Eanverness','Satbury')],
{'Appleby':(58,189), 'Berkton':(84,13),
'Northpass':(84,45),'Satbury':(25,181),
'Eanverness':(134,50),}, [18,21,24,48]
)
#To verify, if we are getting the correct output
for objects in output:
print(objects.sender,objects.recipient,objects.cost,objects.distance)
Output:
Appleby Satbury 18 33.95585369269929
Northpass Berkton 18 32.0
Eanverness Satbury 21 170.41713528867922
Note: Please check for indentation error as pasting the code may cause some changes in that.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.