Why does a `RunWorkout` go anywhere a `Workout` goes but never the reverse — and what is the third number you pass positionally?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
One list can hold plain Workout objects and RunWorkout objects together, and every one of them answers get_calories(). Write the helper that adds up the calories of every object in such a list, whatever kind each one is. Define total_calories(workouts) so that it returns the total.
from datetime import datetime
class Workout(object):
cal_per_hr = 200
def __init__(self, start, end, calories=None):
self.start = datetime.strptime(start, '%m/%d/%Y %I:%M %p')
self.end = datetime.strptime(end, '%m/%d/%Y %I:%M %p')
self.calories = calories
def get_duration(self):
return (self.end - self.start).total_seconds() / 3600
def get_calories(self):
if self.calories is None:
return Workout.cal_per_hr * self.get_duration()
return self.calories
class RunWorkout(Workout):
def __init__(self, start, end, elev=0, calories=None, route_gps_points=None):
Workout.__init__(self, start, end, calories)
self.elev = elev
self.route_gps_points = route_gps_points
def get_elev(self):
return self.elevSubstituting a subclass instance for its parent
State that a RunWorkout serves anywhere a Workout does while the reverse fails, and write the two near-identical helpers total_calories and total_elevation side by side.
Three calls to the summing helpers
Write total_calories([w1,w2,rw1,rw2]) as 1000.0 from 100 + 100 + 400 + 400, total_elevation([rw1,rw2]) as 300, and the AttributeError that total_elevation([w1,rw1]) raises.
Positional order, holes, and named parameters
Record the order start, end, elevation, calories, that a skipped default may not be left as an empty comma, and trace what a third positional number and calories=300 each produce.