Year 2025
Role Developer
Status Complete

About

Python code to exemplify inheritance and sub classes.

Technologies

Python Sub Classes Inheritance
Source Code Python
class Animal:
    def __init__(self, animal, time, distance):
        self.animal = animal
        self.time = time
        self.distance = distance
    def speed(self):
        fast = self.distance/self.time
        print("The {} ran {} in {} for a speed of {:.2f}m/s".format(self.animal, self.distance, self.time, fast))
        
class Person(Animal):
    def __init__(self, time, distance, name):
        super().__init__(self, time, distance)
        self.name = name
    def speed(self):
        fast = self.distance/self.time
        print("{} ran {} in {} for a speed of {:.2f}m/s".format(self.name, self.distance, self.time, fast))
        

animalList = []

while True:
    answer1 = input("Would you like to enter a 1) Human or 2) Animal 3) Exit: ")
    if answer1 == '1':
        time = float(input("What was their time: "))
        distance = int(input("What was the distance in meters: "))
        name = input("What is their name: ")
        p1 = Person( time, distance, name)
        animalList.append(p1)
    elif answer1 == '2':
        animal = input("What was the animal: ")
        time = float(input("What was their time: "))
        distance = int(input("What was the distance in meters: "))
        a1 = Animal(animal, time, distance)
        animalList.append(a1)
    else:
        break

for animal in animalList:
    animal.speed()