1. 程式人生 > >python 練習題之 貓

python 練習題之 貓

class Cat:

    def __init__(self,name=None,age=None,color=None):
        self.__name=name
        self.__age=age
        self.__color=color

    def setName(self,name):
        self.__name=name
    def setAge(self,age):
        if age<0:
            print("年齡不合法")
            self.__age=0
        else
: self.__age = age def setColor(self,color): self.__color=color # def setvalue(self,name,age,color): # self.__name=name # self.__age=age # self.__color=color def getName(self): return self.__name def show(self): print("姓名:%s年齡:%d,顏色:%s
" %(self.__name,self.__age,self.__color)) c1=Cat("小花",10,'白色') # c1.setName("小花") # c1.setAge(-10) # c1.setColor("白色") c1.show() c2=Cat() c2.setName("小白") c2.setAge(2) c2.setColor("花色") c2.show() while True: name = input("請輸入姓名:") if name == c1.getName(): print("有這隻貓"
) c1.show() break elif name == c2.getName(): print("有這隻貓") c2.show() break else: print("沒有")