首页 > 解决方案 > 一次只有一个调用函数在工作

问题描述

我有点迷茫,我修改了一个程序以从文本文件中读取,但是当我尝试运行该程序时,它只运行一个调用函数,并在第三个调用函数上出错。当我一次运行它们时,它们会找到,你能帮我弄清楚我做错了什么吗?

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_\OneDrive\Documents \Python\第 6 周作业 1.py”,第 74 行,主要部分(opencourse)文件“C:\Users\lord_\OneDrive\Documents\Python\第 6 周作业 1.py”,第 52 行,第 3 部分 maxlength = len(max(lines, key=len)) ValueError: max() arg 是一个空序列

标签: python

解决方案


opencourse对象是一个迭代器;每次阅读时,它都会在内部跟踪您所阅读的内容,一旦您阅读了所有内容,您就无法再阅读了。当您for aline in courses在第一个函数中执行时,您正在阅读整个迭代器,之后它基本上是“空的”。然后,您将相同的迭代器传递给您的其他函数,它们无事可做,因为没有什么可读取的了。

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


推荐阅读