如何使用list作为函数的参数

时间:2015-03-19 23:16:10

标签: python

我的第一个函数从输入文件创建一个列表。我正在尝试使用我创建的列表作为第二个函数的参数。我该怎么做?我知道每个函数都有自己的命名空间,所以我做错了。我假设我需要在全局命名空间中分配这个变量。

def get_data(file_object):
    while True:
        try:
            file_object=input("Enter the name of the input file: ")
            input_file=open(file_object, "r")
            break
        except FileNotFoundError:
            print("Error: file not found\n:")
    student_db=[]
    for line in input_file:
        fields=(line.split())
        name=int(fields[0])
        exam1=int(fields[1])
        exam2=int(fields[2])
        exam3=int(fields[3])
        in_class=int(fields[4])
        projects=int(fields[5])
        exercises=int(fields[6])
        record=[name,exam1,exam2,exam3,in_class,projects,exercises]
        student_db.append(record)
    student_db.sort()
    return student_db



#def calculate_grade(a_list):
#    print(a_list)
#how do I use student_db as a parameter??





def main():
#    a_list=student_db
#    b=calculate_grade(a_list)
#    print(b)
    a=get_data("data.tiny.txt")
    print(a)

这是我正在使用的输入文件

  031   97   108   113    48   217    14
  032   97   124   147    45   355    15
  033  140   145   175    50   446    14
  034  133   123   115    46   430    15
  035  107    92   136    45   278    13
  036   98   115   130    37   387    15
  037  117    69   131    34   238    12
  038  134   125   132    50   434    15
  039  125   116   178    50   433    15
  040  125   142   156    50   363    15
  041   77    51    68    45   219    15
  042  122   142   182    50   447    15
  043  103   123   102    46   320    15
  044  106   100   127    50   362    15
  045  125   110   140    50   396    15
  046  120    98   129    48   325    13
  047   89    70    80    46   302    14
  048   99   130   103    50   436    15
  049  100    87   148    17   408    13
  050  104    47    91    37    50     9

3 个答案:

答案 0 :(得分:1)

您的主要(和注释掉的代码)应如下所示:

def calculate_grade(a_list):
    print(a_list)


def main():
    a_list=get_data("data.tiny.txt")
    calculate_grade(a_list)
main()

记住这一点:

如果您的函数返回值。然后你将它分配给全局命名空间中的变量并在不同的点使用它。如果它有打印语句,则当您调用时,您无需再次使用print

答案 1 :(得分:0)

在您的示例中,student_db(如我所知)存储在变量a中。您可以将该变量传递给第二个函数,因此只需将calculate_grade(a)添加到主函数中(显然在定义它之后)。

答案 2 :(得分:0)

函数get_data()返回一个列表,该列表可以分配给局部变量并传递给其他函数。就像你现在这样做。

a=get_data("data.tiny.txt")
calculate_grade(a)

我们不能直接使用student_db [],因为它是get_data()的本地。它可以声明/使用为全局,但最终它们会使您的程序不那么灵活,并且更容易包含难以发现的错误。

其他方法是使用方法(面向对象机制)。

class FileList :
    def get_data(self, file_object):
        while True:
            try:
                file_object=input("Enter the name of the input file: ")
                input_file=open(file_object, "r")
                break
            except FileNotFoundError:
                print("Error: file not found\n:")

        self.student_db=[]
        for line in input_file:
            fields=(line.split())
            name=int(fields[0])
            exam1=int(fields[1])
            exam2=int(fields[2])
            exam3=int(fields[3])
            in_class=int(fields[4])
            projects=int(fields[5])
            exercises=int(fields[6])
            record=[name,exam1,exam2,exam3,in_class,projects,exercises]
            self.student_db.append(record)
        self.student_db.sort()


    def print_object(self):
        print self.student_db

def main():
    myobj=FileList()
    myobj.get_data("data.tiny.txt")
    myobj.print_object()
相关问题