需要一个循环的帮助

时间:2017-07-29 02:13:17

标签: python-3.x

我想知道如何将这些项放入循环来缩短代码?应该使用while循环还是for循环。另外,我将如何使用.split()。这是我到目前为止所做的,但似乎有点长,并减少了一点。

def printReport(line):
    #   Prints the heading to show the test scores
    print("__________________________________________________________")
    print("Student Name     Score 1     Score 2     Score 3     Total")
    print("----------------------------------------------------------")

    name = []       #   Declare name a list
    score1 = []      #   Declare test1 a list
    score2 = []      #   Declare test2 a list
    score3 = []      #   Declare test a list

    with open("grades.txt", "r") as f:
        for line in f:
            name.append(line.split(",", 1)[0])
        line = name[0]
        capacity = len(name)
        index = 0
        while index != capacity:

            line = name[index]
            for nameOut in line.split():

                print(nameOut)
                index = index + 1
    with open("grades.txt", "r") as f:
            for line in f:
                score1.append(line.split(",", -1)[1])
            line = score1[0]
            capacity = len(score1)
            index1 = 0
            while index1 != capacity:
                line = score1[index1]
                for s1Out in line.split():

                    print(s1Out)
                    index1 += 1    
    with open("grades.txt", "r") as f:
            for line in f:

                score2.append(line.split(",", -1)[2])
            line = score2[1]
            capacity = len(score2)
            index2 = 0
            while index2 != capacity:
                line = score2[index2]
                for s2Out in line.split():
                    print(s2Out)
                    index2 += 1
    with open("grades.txt", "r") as f:
            for line in f:
                score3.append(line.split(" ", -1)[3])
            line = score3[2]
            capacity = len(score3)
            index3 = 0
            while index != capacity:
                line = score3[index3]
                for s3Out in line.split():
                    print(s3Out)
                    index3 += 1

3 个答案:

答案 0 :(得分:1)

一个功能怎么样?

def routine(score, index, sOut, n):
    with open("grades.txt", "r") as f:
        for line in f:
            score.append(line.split(",", -1)[n])
        line = score[0]
        capacity = len(score)
        index = 0
        while index != capacity:
            line = score[index]
            for sOut in line.split():

                print(sOut)
                index += 1 

routine(score1, index1, s1Out, 1)
routine(score2, index2, s2Out, 2)
routine(score3, index3, s3Out, 3)

编辑:请不要复制并粘贴这个,因为我可能有一个错误的变量名称,但这是它的主旨,你必须执行。

Edit2:这是一个嵌套函数

def outer():
    def inner():
        pass

答案 1 :(得分:0)

看起来像家庭作业,但这里有一个暗示。读取文件一次并处理类似于以下内容的行:

>>> line = "name,score1,score2,score3"
>>> name,*scores = line.split(',')
>>> name
'name'
>>> scores
['score1', 'score2', 'score3']

该行分为四个列表。 name,*scores语法将列表中的第一项内容分配给name,其余内容分配给scores

答案 2 :(得分:0)

看起来你做了很多工作可能会更容易。

你还没有告诉你的'grades.txt'文件是什么样的。所以我们假设它看起来像这样:

Mark, 100, 99, 89
Sam, 87, 75, 70

如果是这种情况,您不需要一遍又一遍地读取文件 - 这很慢。您只需读取一次文件并处理每一行。例如:

with open("grades.txt", "r") as f:
    for line in f:
        line = line.strip()   #strip off newline character
        arr = line.split(",") #split your fields on the comma
        arr.append(sum([int(x) for x in arr[1:]])) #make the sum of the scores
        print('{:20}{:10}{:10}{:10}{:>15}'.format(*arr)) #print using the format feature of python

{:20}这样的格式告诉print语句使这个字段宽20个字符。这样,即使是不同长度的名称,表格也会排列。

这将产生格式良好的表格。像这样:

_________________________________________________________________
Student Name        Score 1   Score 2   Score 3             Total
_________________________________________________________________
Mark                 100       99        89                   288
Sam                  87        75        70                   232
相关问题