Список возврата Python - PullRequest
0 голосов
/ 22 мая 2018
class alexicon(object):

def __init__(self):
    self.directions = ['north', 'south', 'west', 'east', 'up', 'down']
    self.items = []

def scan(self, words):
    self.word = words.split()
    # direction
    self.direction_word = [i for i in self.word if i in self.directions]
    self.direction_list = []
    for x in self.direction_word:
        self.direction_list.append(('direction', '%s' %(x)) )
    if self.direction_list != []:
        self.items.extend(self.direction_list)
    else:
        pass
   return self.items

lexicon = alexicon()
result = lexicon.scan('north')
print result

Почему результат печати получает «Нет»?Как я могу получить список предметов?Но если я распечатаю lexicon.items, я могу получить правильный список (в этом списке есть элементы).

1 Ответ

0 голосов
/ 22 мая 2018

Вам нужно изменить отступы:

class alexicon(object):

    def __init__(self):
        self.directions = ['north', 'south', 'west', 'east', 'up', 'down']
        self.items = []

    def scan(self, words):
        self.word = words.split()
# direction
        self.direction_word = [i for i in self.word if i in self.directions]
        self.direction_list = []
        for x in self.direction_word:
            self.direction_list.append(('direction', '%s' %(x)) )
        if self.direction_list != []:
            self.items.extend(self.direction_list)
        else:
            pass
        return(self.items)

lexicon = alexicon()
result = lexicon.scan('north')
print(result)

Вывод:

[('direction', 'north')]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...