python面对对象小案例:
class Person: def __init__(self,name,weight): #self.属性=形参 self.name=name self.weight=weight def __str__(self): return '我的名字叫做:%s,体重是:%.2f公斤'% (self.name,self.weight) def run(self): print(f'{self.name},爱跑步,此时的体重:{self.weight-0.5}公斤') self.weight -= 0.5 pass def eat(self): print(f'{self.name},爱吃饭,此时的体重:{self.weight+1}公斤') self.weight += 1 passxiaoming=Person('小明',75.0)while True: run_eat=input('请输入您需要的操作:') if run_eat=='跑步': xiaoming.run() if run_eat=="吃饭": xiaoming.eat()