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

求解~谢谢
展开
 我来答
Lovesoo
推荐于2018-02-26 · TA获得超过2385个赞
知道小有建树答主
回答量:654
采纳率:66%
帮助的人:413万
展开全部

__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)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式