Python出现错误:builtins.TypeError: object() takes no parameters
#Easiertoaskforgiveness(After)classNegativeAgeError(Exception):#Icanpass#Morepowerful...
# Easier to ask forgiveness (After)
class NegativeAgeError(Exception): # I can
pass # More powerful than break
class Person:
def __int__(self, name, age):
"""(Person, str, int) -> NoneType
Create a new person with the given name with the given name and age.
Age should be a non-negative integer
"""
self.name = name
if age < 0:
raise NegativeAgeError
else:
self.age = age
# Look before leaping
def make_person(age):
if age < 0:
return None
elif age > 200:
return None
else:
return Person('a', age)
当我输入
make_person(20)的时候,他说:
Traceback (most recent call last):
File "/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py", line 25, in make_person
builtins.TypeError: object() takes no parameters
求解~谢谢 展开
class NegativeAgeError(Exception): # I can
pass # More powerful than break
class Person:
def __int__(self, name, age):
"""(Person, str, int) -> NoneType
Create a new person with the given name with the given name and age.
Age should be a non-negative integer
"""
self.name = name
if age < 0:
raise NegativeAgeError
else:
self.age = age
# Look before leaping
def make_person(age):
if age < 0:
return None
elif age > 200:
return None
else:
return Person('a', age)
当我输入
make_person(20)的时候,他说:
Traceback (most recent call last):
File "/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py", line 25, in make_person
builtins.TypeError: object() takes no parameters
求解~谢谢 展开
1个回答
展开全部
__init__函数写错成__int__了:
class Person:
def __init__(self, name, age):
self.name = name
if age < 0:
raise NegativeAgeError
else:
self.age = age
print self.name,self.age
def make_person(self,age):
if age < 0:
return None
elif age > 200:
return None
else:
return Person('a', age)
test=Person('haha',20)
test.make_person(30)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询