-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9animal.py
65 lines (52 loc) · 1.62 KB
/
9animal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 9. Create a base class called “Animal” and two subclasses:
# “Dog” & “Cat.” Add methods & attributes specific to each subclass.
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def make_sound(self):
pass
class Dog(Animal):
def __init__(self, name, age, breed):
super().__init__(name, age)
self.breed = breed
def make_sound(self):
return "Woof! Woof!"
def wag_tail(self):
return f"{self.name} is wagging its tail."
class Cat(Animal):
def __init__(self, name, age, color):
super().__init__(name, age)
self.color = color
def make_sound(self):
return "Meow!"
def purr(self):
return f"{self.name} is purring."
def get_user_input(prompt):
return input(prompt)
# Get user input for Dog instance
user_dog = Dog(
get_user_input("Enter the name of the dog: "),
get_user_input("Enter the age of the dog: "),
get_user_input("Enter the breed of the dog: ")
)
# Get user input for Cat instance
mycat = Cat(
get_user_input("Enter the name of the cat: "),
get_user_input("Enter the age of the cat: "),
get_user_input("Enter the color of the cat: ")
)
# Displaying information
print("\n----- Dog Details -----")
print("Name:", user_dog.name)
print("Age:", user_dog.age)
print("Breed:", user_dog.breed)
print("Sound:", user_dog.make_sound())
print("Tail Wagging:", user_dog.wag_tail())
print()
print("----- Cat Details -----")
print("Name:", mycat.name)
print("Age:", mycat.age)
print("Color:", mycat.color)
print("Sound:", mycat.make_sound())
print("Purring:", mycat.purr())