Python 一次只能使用一个调用函数

Python 一次只能使用一个调用函数,python,Python,我有点不知所措,我修改了一个程序来读取文本文件,但当我试图运行该程序时,它只运行一个调用函数,第三个调用函数出错。当我一次一个地运行他们的工作时,你能帮我找出我做错了什么吗 import sys def spacejam(diff3): '''Used to create spaces between the end of the max length and the students''' if len(diff3) == 23: space = (&q

我有点不知所措,我修改了一个程序来读取文本文件,但当我试图运行该程序时,它只运行一个调用函数,第三个调用函数出错。当我一次一个地运行他们的工作时,你能帮我找出我做错了什么吗

import sys
def spacejam(diff3):
        '''Used to create spaces between the end of the max length and the students'''
    if len(diff3) == 23:
        space = (" " * 2)
    else:
        space = (" " * (25 - len(diff3)))
return space

def part1(courses):
'''Used to create table 1 of the assignment (printing Department Code,
   Course Number, and Students and finding the total number of students)'''
    total = 0
    for aline in courses: #for loop to check each line of the text file
        values = aline.split() #splits each line into elements
        diff = values[:2] #Sets diff to elements 0-1
        diff = ''.join(diff) #joins elements 0 and 1
        temp = values[-1] #sets temp to the last element
        temp = int(temp)   #makes the last elemet a interger
        total = total + temp #Adds the total of the intergers
        print(diff, values[-1].rjust(4)) #prints the needed values

     print("Total number of students: ", total)  # prints total

def part2(courses):
    '''Used to create table 2 of the assignment (printing Department Code,
    Course Number, Title, and Students)
    limiting the length of the Title to 23 characters'''
    for items in courses:  # For loop for every element in courses
        diff = items.split()  # Splits the elements in courses
        diff2 = diff[2:-1]  # combines elements 2 through -1(not including the last item)
        sep = ' '  # Sets the seperator to space
        diff3 = sep.join(diff2)  # Joins elements 2 through -1 as a string
        diff3 = ('%.23s' % diff3)  # Sets the character limit to 23 and shortens strings over 23 
         #characters
    print(diff[0], diff[1], diff3, spacejam(diff3), diff[-1].rjust(4))  # Prints the results

def part3(courses):
    '''Used to create table 2 of the assignment (printing Department Code,
    Course Number, Title, and Students) sorting the elements in a list and
    lining up the number of students'''
    lines = courses.readlines()
    maxlength = len(max(lines, key=len))

    for items in lines:  # For loop for every element in courses
        diff = items.split()  # Splits the each element in course
        diff2 = diff[:-1]  # Combines elements back together except the last element (-1)
        sep = ' '  # Sets the seperator to space
        diff3 = sep.join(diff2)  # Joins elements except the last element(-1) to a string
        space = (" " * (maxlength - len(diff3)))  # Add spaces between the elements based on the 
       #maxlength
    print(diff3, space, diff[-1].rjust(4))  # Prints results



def main():
    sys.setrecursionlimit(3500000)
    opencourse = open('courses.txt', 'r') #opens and reads a text file

    part1(opencourse)   #calls function part 1
    print('')

    part2(opencourse)  #calls function part 2
    print('')

    part3(opencourse)
    opencourse.close()  #closes opened text file
    main()
下面是我运行此操作时发生的情况:

CS152   21 
CS369    8
CS365  119
CS208   24
CS319   14
MA221   12
MA311    7
MA150   27
CS335   20
IS361   22
MG315    6
Total number of students:  280
回溯(最近一次调用上次):文件 “C:\Users\lord\OneDrive\Documents\Python\Week 6 assignment 1.py”, 第76行,输入 main()文件“C:\Users\lord\uOneDrive\Documents\Python\Week 6 assignment 1.py”,第74行,在main中 第三部分(opencourse)文件“C:\Users\lord\OneDrive\Documents\Python\Week 6 assignment 1.py”, 第52行,第3部分 maxlength=len(max(lines,key=len))值错误:max()arg是一个空序列


opencourse
对象是迭代器;每次你读它的时候,它都会在内部跟踪你读过的内容,一旦你读了所有的东西,你就不能再读了。当你在courses中对aline执行
操作时,你在第一个函数中阅读整个迭代器,之后它基本上是“空的”。然后将同一个迭代器传递给其他函数,它们没有什么可做的,因为已经没有什么可读的了


与其将file对象传递给每个函数,我建议先使用
readlines()
将其转换为字符串列表,然后将该列表传递给每个函数;与迭代器不同,遍历列表不会改变列表。

请将代码重新格式化为有效代码(不要以
''
开头),这非常有意义。对不起,这个愚蠢的问题。@Zelow这不是一个愚蠢的问题。总的来说,我认为这是一个相当好的问题:它包括了一个可复制的例子,预期的与实际的行为,并且可以用一个简洁但不琐碎的答案来回答。