Список пустых внешних функций - Python - PullRequest
0 голосов
/ 21 марта 2019

Я пытаюсь скопировать строки текстового файла в список в одной функции, а затем распечатать содержимое этого списка во 2-й функции.
Если я поместил оператор print в первую функцию, он выведетправильный список, но если я пытаюсь распечатать с помощью 2-й функции, он выводит пустой список.

Как передать список из моей 1-й функции в мою 2-ю функцию?

Мой текущий код:

def selectfile():
    userselect = input("Please type the filename, for an example you can type: numbers.txt or 10lines.txt: ")
    file = open(userselect, "r") #  grabs the user input from above and opens the selected file
    return file # puts the value into selectfile func


def TxttoList():  # starts the paste function
    file = selectfile()  # grabs the info from the function above
    lines = file.readlines()[1:]  # starts loading in the content line by line
    newlist = []  # declares my list
    for line in lines:  # loops through each line
        content = line.strip().split("\t")  # uses tab as separator
        newlist.append(content)  # adds the lines to the list
 #  print(newlist) //Lists prints as expected here if i remove the #, but I need the list in the function below. 
    return newlist


def TopScore():  # This is where I need the list I created above
    newlist = TxttoList() 
    print(newlist)  

TxttoList()  # starts the program ```

Результат, который я ожидал:

[Петр, 22] [Ханс, 11] [Ларс, 20] [Флеминг, 21] [Дорте, 19] [Бо, 11]

Результат, который я получил:

Любая помощь высоко ценится.

1 Ответ

0 голосов
/ 21 марта 2019

Вы вызываете TxttoList (), который действительно не печатает.Разве вы не хотели вызывать TopScore () в последней строке?

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