2017-10-07 5 views
0

J'ai une question que je ne peux pas résoudre. Ceci est mon code:AttributeError: L'objet 'NoneType' n'a pas d'attribut '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 

Lorsque j'utilise chain.get_nth(4), par exemple, il montre que:

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

Voici mon code après avoir changé:

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 

Mais encore ne fonctionne pas. Pourquoi ça ne marche pas et comment puis-je le réparer? Merci beaucoup.

+0

Quel est le code d'appel? –

+0

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

+0

Y at-il vraiment assez de personnes dans votre chaîne? – user2357112

Répondre

0

Je pense que vous avez mal compris.

Votre PeopleChain classe:

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

self.leader = Aucun ## ??

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 

Juste pour dire, le type (Aucun) est égal à NoneType. Au lieu d'utiliser

self.leader = None 

utilisation:

self.leader = [] 
0

Essayez le code suivant

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 

Au lieu d'ajouter instruction if vous pouvez ajouter essayer et attraper est beaucoup plus pythonique. Donc, votre code deviendra

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

maintenant sur l'exécution de ce code comme ceci

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

Il lancera une exception d'erreur personnalisée telle que

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