求助为什么有错,初学者呜呜呜
呜呜呜救救孩子 你要学会自己看把报错内容,人家都把错误的语句给你提示出来了 __init__双下划线的。。。。我也看不清你那个是几个下划线,看提示 可能是这个问题
init 前后应是双下滑线而不是单下滑线,__init__,我看着你的代码像单的 错的有点多,把代码发出来吧,好改 挑灯看花 发表于 2020-5-15 15:28
错的有点多,把代码发出来吧,好改
class Person():
def __init__(self,name,weight):
self.name = name
self.weight = weight
print(self.name + "体重是" + "Kg")
def run(self,changing_weight):
print(self.name + "正在跑步...")
self.changing_weight = changing_weight - 0.5
print("体重是" + str(self.changing_weight))
def eat(self,chang_weight):
print(self.name + "正在吃东西...")
self.chang_weight = chang_weight + 1
print("体重是" + str(self.chang_weight))
xiaoming = Person('xiaoming','75.00')
print(xiaoming.run())
xiaomei = Person('小美','45.00')
print(xiaomei.eat())
dongguanglxl 发表于 2020-5-15 15:31
class Person():
def __init__(self,name,weight):
self.name = name
按你的代码应该是这个意思吧
class Person:
def __init__(self,name,weight):
self.name = name
self.weight = weight
print(self.name + "体重是{}Kg".format(self.weight))
def run(self,changing_weight):
print(self.name + "正在跑步...")
self.weight -= changing_weight
print("体重是" + str(self.weight))
def eat(self,chang_weight):
print(self.name + "正在吃东西...")
self.weight += chang_weight
print("体重是" + str(self.weight))
xiaoming = Person('xiaoming',75.00)
xiaoming.run(5)
xiaomei = Person('小美',45.00)
xiaomei.eat(5) 挑灯看花 发表于 2020-5-15 15:36
按你的代码应该是这个意思吧
class Person:
def __init__(self,name,wei ...
谢谢您啦恩呢 self自己只有weight属性,那两个跑步吃饭的函数你用的体重属性是未定义的。。 class Person():
def __init__(self, name, weight):
self.name = name
self.weight = weight
print(self.name + "体重是" +self.weight+ "Kg")
def run(self, changing_weight):
print(self.name + "正在跑步...")
self.changing_weight = changing_weight - 0.5
print("体重是" + str(self.changing_weight))
def eat(self, chang_weight):
print(self.name + "正在吃东西...")
self.chang_weight = chang_weight + 1
print("体重是" + str(self.chang_weight))
xiaoming = Person('xiaoming', '75.00')
xiaoming.run(75)
xiaomei = Person('小美', '45.00')
xiaomei.eat(45)
初始化用双下划线
class带methon即是执行函数,不用print
methon没带参数
页:
[1]
2