1 def asd1(asd): 2 def dsa1(): 3 print("----猫的信息如下----") 4 asd() 5 6 return dsa1 7 8 9 def asd12(asd): 10 def dsa1(): 11 print("----检查有没有这个猫----") 12 asd() 13 14 return dsa1 15 16 17 class Cat: 18 def __init__(self): 19 self.name = "" 20 self.age = 0 21 self.color = "" 22 23 def show1(self): 24 print(self.name, "在跑") 25 26 def show2(self): 27 print(self.name, "在叫") 28 29 def show(self): 30 print("姓名:%s\t\t年龄:%d\t\t颜色:%s" 31 % (self.name, self.age, self.color)) 32 33 34 # 这是输入猫的信息代码 35 cdata = [] 36 num = 0 37 a = 0 38 while True: 39 cat_data = Cat() 40 cat_data.name = input("猫的名字\n") 41 for i in cdata: 42 if i == cat_data.name: 43 print("猫名字已经存在") 44 a = 1 45 break 46 if a == 1: 47 a -= 1 48 continue 49 cdata.append(cat_data.name) 50 cat_data.age = int(input("猫的年龄\n")) 51 cdata.append(cat_data.age) 52 cat_data.color = input("猫的颜色\n") 53 cdata.append(cat_data.color) 54 print(cat_data.name, "已经存储完,退出按 0,继续按 回车") 55 a = input() 56 num += 1 57 if a == "0": 58 break 59 else: 60 continue 61 # 这是遍历猫的信息 62 num1 = 0 63 for i in range(0, len(cdata), 3): 64 @asd1 65 def asd(): 66 global num1 67 cat_data = Cat() 68 cat_data.name = str(cdata[num1]) 69 num1 += 1 70 cat_data.age = int(cdata[num1]) 71 num1 += 1 72 cat_data.color = str(cdata[num1]) 73 num1 += 1 74 cat_data.show() 75 cat_data.show1() 76 cat_data.show2() 77 78 79 asd() 80 # 这是猫的年龄总和 81 num2 = 1 82 cat_ages = 0 83 times = 0 84 for i in range(0, len(cdata), 1): 85 cat_ages += cdata[num2] 86 num2 += 3 87 times += 1 88 if num2 > len(cdata): 89 break 90 print(times, "只猫的年龄之和为:", cat_ages) 91 # 这是检查猫资料是否存在 92 exit1 = 0 93 while True: 94 @asd12 95 def maomao(): 96 global exit1 97 i = 0 98 a = input("请输入要检查的名字") 99 for j in cdata: 100 if a == j: 101 print("找到这个猫的信息如下") 102 print("----猫的信息如下----") 103 cat_data = Cat() 104 cat_data.name = str(cdata[i]) 105 cat_data.age = int(cdata[i + 1]) 106 cat_data.color = str(cdata[i + 2]) 107 cat_data.show() 108 break 109 i += 1 110 else: 111 print("没有这个猫,程序结束") 112 exit1 += 1 113 114 115 maomao() 116 if exit1 == 1: 117 break
老师的代码
1 class Cat: 2 def __init__(self,name,age,color): 3 self.name = name 4 self.age = age 5 self.color = color 6 7 def run(self): 8 print("%s在跑步" % self.name) 9 10 def shout(self): 11 print("%s在叫" % self.name) 12 13 def show(self): 14 print("姓名:%s,年龄:%d,颜色:%s" 15 % (self.name, self.age, self.color)) 16 cat_list = [] 17 sum = 0 18 for i in range(3): 19 name=input("请输入姓名") 20 age=int(input('年龄')) 21 color=input("颜色") 22 c1 = Cat(name,age,color) 23 cat_list.append(c1) 24 sum += c1.age 25 26 print("猫的信息如下") 27 for i in cat_list: 28 i.show() 29 i.run() 30 i.shout() 31 32 print("年龄总和是:", sum) 33 34 print("-----检测----") 35 while True: 36 name = input("请输入要找的猫的姓名:") 37 for i in cat_list: 38 if name == i.name: 39 i.show() 40 break 41 42 else: 43 print("没有找到") 44 break