2017-10-07 147 views
0

我有一个问题,我无法解决。这是我的代码:AttributeError:'NoneType'对象没有属性'name'?

class Person: 
    def __init__(self, name): 
    self.name = name 
    self.next = None 

class PeopleChain: 
    def __init__(self, names): 
    if names == []: 
     self.leader = None 
    else: 
     self.leader = Person(names[0]) 
     current_person = self.leader 
     for name in names[1:]: 
     current_person.next = Person(name) 
     current_person = current_person.next 
    def get_nth(self, n): 
    """Return the name of the n-th person in the chain. 
    >>> chain = PeopleChain(['a', 'b', 'c']) 
    >>> chain.get_nth(1) 
    'a' 
    """ 
    current_person = self.leader 
    for i in range(1, n): 
     if i < n: 
     current_person = current_person.next 
    return current_person.name 

当我使用chain.get_nth(4),例如,它表明:

AttributeError: 'NoneType' object has no attribute 'name' .

这里是我的代码后,我改变了它:

def get_nth(self, n): 
    current_person = self.leader 
    for i in range(1, n): 
     if i < n: 
      current_person = current_person.next 
      if current_person is None: 
       raise SomeError #user-defined error 
    return current_person.name 

但它仍然不起作用。为什么它不起作用,我该如何解决它?非常感谢你。

+0

什么是调用代码? –

+0

chain = PeopleChain(['a','b','c']) chain.get_nth(4) – user56309

+0

链中是否有足够的人? – user2357112

回答

0

我想你误会了。

PeopleChain类:

class PeopleChain: 
def __init__(self, names): 
    if names == []: 

self.leader =无 ##?

else: 
     self.leader = Person(names[0]) 
     current_person = self.leader 
     for name in names[1:]: 
      current_person.next = Person(name) 
      current_person = current_person.next 
def get_nth(self, n): 
"""Return the name of the n-th person in the chain. 
>>> chain = PeopleChain(['a', 'b', 'c']) 
>>> chain.get_nth(1) 
'a' 
""" 
current_person = self.leader 
for i in range(1, n): 
    if i < n: 
     current_person = current_person.next 
return current_person.name 

只是说, 型(无) 等于NoneType。除了使用

self.leader = None 

使用:

self.leader = [] 
0

尝试而不是添加if语句,你可以加试,并抓住它更Python的方式的下面的代码

class ShortChainError(Exception): 
    def __init__(self,*args,**kwargs): 
     Exception.__init__(self,*args,**kwargs) 

class Person: 
    def __init__(self, name): 
    self.name = name 
    self.next = None 

class PeopleChain: 
    def __init__(self, names): 
    if names == []: 
     self.leader = None 
    else: 
     self.leader = Person(names[0]) 
     current_person = self.leader 
     for name in names[1:]: 
     current_person.next = Person(name) 
     current_person = current_person.next 
    def get_nth(self, n): 
    """Return the name of the n-th person in the chain. 
    >>> chain = PeopleChain(['a', 'b', 'c']) 
    >>> chain.get_nth(1) 
    'a' 
    """ 
    current_person = self.leader 
    for i in range(1, n): 
     if i < n: 
     try: 
      current_person = current_person.next 
      name = current_person.name 
     except AttributeError: 
      raise ShortChainError("Your Message Here!!!") 
    return name 

。所以,你的代码将成为

if i < n: 
    try: 
     current_person = current_person.next 
     name = current_person.name 
    except AttributeError: 
     raise ShortChainError("Your Message Here!!!") 
return name 

现在在运行这段代码这样

PeopleChain(['a', 'b', 'c']).get_nth(4) 

这将引发自定义错误异常如

raise ShortChainError("Your Message Here!!!") 
__main__.ShortChainError: Your Message Here!!!