Python实战训练营1


九九乘法表


射雕英雄传题


鸡兔同笼

创建一个动物类

class Animal():
# 构造函数
def init(self, name, color, age, sex):
self.name = name
self.color = color
self.age = age
self.sex = sex

# 会跑类方法
def run(self):
    print(f"{self.name}会跑")

# 会叫类方法
def speak(self):
    print(f"{self.name}会叫")

创建子类猫

class Cat(Animal):
def init(self, name, color, age, sex):
super().init(name, color, age, sex)
self.pur = “短发”

def Canmouse(self):
    print(f"{self.name}会捉老鼠")

def Catspeak(self):
    print(f"{self.name}喵喵叫")

创建子类狗

class Dog(Animal):
def init(self, name, color, age, sex):
super().init(name, color, age, sex)
self.fur = “长毛”

def CanHouse(self):
    print(f"{self.name}会看家")

def Dogspeak(self):
    print(f"{self.name}汪汪叫")

if name == ‘main’:
Cat=Cat(“TOM猫”,“花色”,“6岁”,“男猫”)
Cat.Canmouse()
print(Cat.name,Cat.color,Cat.sex,Cat.age,Cat.pur)

Dog=Dog("藏獒","纯黑色","10岁","男狗")
Dog.CanHouse()
print(Dog.name,Dog.fur,Dog.sex,Dog.age,Dog.color)