兄弟们,这错哪里了呀?怎么改呀?实在不会了!
毕业设计
1
class Dog():
"""this class initialize a class of a dog"""
def init(self,name,age,master='Rose'):
self.name = name
self.a = age
self.m = master
self.typee = 'animal'
def sit(self):
print(self.name + " is sitting.")
def roll_over(self):
print(self.name + " rolled over.")
def master(self):
print(self.name + " 's master is " + self.m)
def type(self):
print(self.name + " 's type is " + self.typee + ".")
def age(self):
print(self.name + " 's age is " + self.a)
def met(self):
print("This is a test!!!")
dog1=Dog("宝贝")
dog1.sit()
dog1.roll_over()
dog1.master()
dog1.type()
dog1.age()
dog1.met()
错误类型:
C:\Users\Administrator\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/pythonProject/突然.py
Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\pythonProject\突然.py", line 21, in <module>
dog1=Dog("宝贝")
TypeError: init() missing 1 required positional argument: 'age'
Process finished with exit code 1
-
python的异常信息很明确:init() missing 1 required positional argument: 'age',init方法丢失了位置参数,age
def init(self,name,age,master='Rose'):
这里四个参数,第一个self不用管,是类对象自己,最后一个默认参数,可给可不给,中间name和age参数,你必须传入,也就是你实例化时,最少要传两个参数,最多三个参数dog1=Dog("宝贝")
,只给了name,没有给age传值,所以报错了 -
两个错误,init前后有下划线,你忽略了,然后传入的必填字段有name和age,你下面的实例化只传入了一个name,age没传
发表回复