Athlete Class Hierarchy

Implement a class Athlete with basic attributes like name, age, and nationality. Then create child classes Runner, Swimmer, Cyclist, each having unique methods and attributes.

Example 1:

Input: Runner('Usain Bolt', 35, 'Jamaican', '100m', '9.58 seconds') 
Output: "Usain Bolt is a 35-year-old Jamaican runner. He runs the 100m in 9.58 seconds."

Example 2:

Input: Swimmer('Michael Phelps', 36, 'American', '100m Butterfly', '49.82 seconds') 
Output: "Michael Phelps is a 36-year-old American swimmer. He swims the 100m Butterfly in 49.82 seconds."

Implement a unique method performance in each of the child classes that returns the respective performance details of the athlete.

class Athlete:
    def __init__(self, name, age, nationality):
        self.name = name
        self.age = age
        self.nationality = nationality

class Runner(Athlete):
    def __init__(self, name, age, nationality, event, record):
        super().__init__(name, age, nationality)
        self.event = event
        self.record = record

    def performance(self):
        return f"{self.name} is a {self.age}-year-old {self.nationality} runner. He runs the {self.event} in {self.record}."

class Swimmer(Athlete):
    def __init__(self, name, age, nationality, event, record):
        super().__init__(name, age, nationality)
        self.event = event
        self.record = record

    def performance(self):
        return f"{self.name} is a {self.age}-year-old {self.nationality} swimmer. He swims the {self.event} in {self.record}."

# Test the classes
athlete1 = Runner('Usain Bolt', 35, 'Jamaican', '100m', '9.58 seconds')
print(athlete1.performance())

athlete2 = Swimmer('Michael Phelps', 36, 'American', '100m Butterfly', '49.82 seconds')
print(athlete2.performance())

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!