Python3, these are on classes. Please help and try to explain it and you go, exa
ID: 3827345 • Letter: P
Question
Python3, these are on classes. Please help and try to explain it and you go, examples are given. Thank you in advanse!!
Train class: Instance variables: string the name of the train name max passengers int the maximum number of passengers the train can transport num passengers int the number of passengers currently on the train speed fps int tt how fast the train travels (in feet per second) Methods: init (self, name, max passengers, speed fps) constructor, initializes all instance variables num passengers should default to 0 when a train is created with the constructor. o Assumptions: speed fps and max passengers will always be a non-negative number name will be a non-empty stringExplanation / Answer
import math
class TrainCapacityException(Exception):
def __init__(self, number, issue="full"):
self.number = number
self.issue = issue
def __str__(self):
if self.issue == "full":
return str(self.number) + " passengers cannot be loaded because the train is full!"
else:
return str(self.number) + " passengers cannot be unloaded because the train is empty!"
class Train:
def __init__(self, name, max_passengers, speed_fps):
self.name = name
self.max_passengers = max_passengers
self.num_passengers = 0
self.speed_fps = speed_fps
def __str__(self):
train = "Train named " + self.name + " with " + str(self.num_passengers)
speed_mps = 0.681818*self.speed_fps
train += " passengers will travel at " + "{0:.2f}".format(speed_mps) + "mph"
return train
def time_to_travel(self, distance_feet):
return distance_feet//self.speed_fps
def load_passengers(self, num_people):
if self.num_passengers + num_people <= self.max_passengers:
self.num_passengers += num_people
else:
over_capacity = (self.num_passengers + num_people) - self.max_passengers
raise TrainCapacityException(over_capacity)
def unload_passengers(self, num_people):
if self.num_passengers - num_people >= 0:
self.num_passengers -= num_people
else:
under_capacity = num_people - self.num_passengers
raise TrainCapacityException(under_capacity, "empty")
# code link: https://paste.ee/p/ytf3Q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.