Exercise

Using dataclasses

Let's put our WeightEntry dataclass we created in the prior exercise to use. We'll create an instance of the WeightEntry for each entry in the weightlog and then use the masstoflipperlength_ratio property we added to perform the calculation. Here is a reminder of our WeightEntry dataclass.

@dataclass
class WeightEntry:
    # Define the fields on the class
    species: str
    flipper_length: int
    body_mass: int
    sex: str

    @property
    def mass_to_flipper_length_ratio(self):
        return self.body_mass / self.flipper_length

Instructions

100 XP
  • Create an empty list called labeled_entries.
  • Iterate over the weight_log entries using tuple expansion to break out species, sex, body_mass, flipper_length.
    • Append a new WeightEntry dataclass instance for each entry to labeled_entries.
  • Print a list of the first 5 mass_to_flipper_length_ratios using a list comprehension.