列出空的外部函数-Python

时间:2019-03-21 18:24:48

标签: python list function

我试图在一个函数中将文本文件的行复制到列表中,然后在第二个函数中打印该列表的内容。
如果我将print语句放在第一个函数中,它将输出正确的列表,但是如果我尝试使用第二个函数进行打印,它将输出一个空列表。

如何将列表从第一个函数传递给第二个函数?

我当前的代码:

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岁] [bo 11岁]

我收到的结果:

  

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您正在调用TxttoList(),它实际上不会打印。 您不是要在最后一行中调用TopScore()吗?