gpt4 book ai didi

python - 让我的输出以某种方式显示时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 01:41:58 26 4
gpt4 key购买 nike

感谢您查看我的问题,并提前感谢您提供的任何帮助。我正在编写一个程序,它从 txt 文件中读取行,然后以某种方式打印输出。他们都在这里

这是我正在阅读的txt文件

JOE FRITZ           AMERICAN GOVERNMENT           B
JOE FRITZ CALCULUS I A
JOE FRITZ COMPUTER PROGRAMMING B
JOE FRITZ ENGLISH COMPOSITION A
LANE SMITH FUND. OF DATA PROCESSING B
LANE SMITH INTERMEDIATE SWIMMING A
LANE SMITH INTRO. TO BUSINESS C
JOHN SPITZ CHOIR C
JOHN SPITZ COLLEGE STATISTICS B
JOHN SPITZ ENGLISH LITERATURE D
JOHN SPITZ INTRO. TO BUSINESS B

我试图让我的输出看起来像这样:

                       GRADE REPORT

NAME COURSE GRADE
-----------------------------------------------------------
JOE FRITZ AMERICAN GOVERNMENT B
CALCULUS I A
COMPUTER PROGRAMMING B
ENGLISH COMPOSITION A
Total courses taken = 4

LANE SMITH FUND. OF DATA PROCESSING B
INTERMEDIATE SWIMMING A
INTRO. TO BUSINESS C
Total courses taken = 3

JOHN SPITZ CHOIR C
COLLEGE STATISTICS B
ENGLISH LITERATURE D
INTRO. TO BUSINESS B
Total courses taken = 4

Total courses taken by all students = 11

Run complete. Press the Enter key to exit.

编辑

感谢您的帮助,我完成了这个程序。

我知道这可能很难看,但我很高兴 ATM 的输出正确。

这是将显示正确输出的源代码:

#-----------------------------------------------------------------------
# VARIABLE DEFINITIONS

name = ""
previousName = ""
course = ""
grade = ""
grandTotal = 0
courseCount = 0
eof = False

#-----------------------------------------------------------------------
# CONSTANT DEFINITIONS

#-----------------------------------------------------------------------
# FUNCTION DEFINITIONS

def startUp():
global gradeFile, grandTotal,courseCount, previousName, name
grandTotal = 0
courseCount = 0
gradeFile = open("grades.txt","r")
print
print ("grade report\n").center(60).upper()
print "name".upper(),"course".rjust(21).upper(),"grade".rjust(33).upper()
print "-" * 60
readRecord()


def readRecord():
global name, course, grade, eof, courseCount

studentRecord = gradeFile.readline()
if studentRecord == "":
eof = True
else:
name = studentRecord[0:20]
course = studentRecord[20:50]
grade = studentRecord[50:51]
eof = False

def processRecords():
global courseCount, previousName, name, grandTotal
while not eof:
if name != previousName:
if name == "JOE FRITZ ":
courseCount = 0
print name + course + " " + grade
previousName = name
courseCount += 1
else:
print "\t\t Total courses taken =",courseCount
print
courseCount = 0
print name + course + " " + grade
previousName = name
courseCount += 1
else:
print (" " * 20) + course + " " + grade
courseCount += 1
grandTotal +=1
readRecord()
print "\t\t Total courses taken =",courseCount


def closeUp():
gradeFile.close()
print "\nTotal courses taken by all students =",grandTotal

#-----------------------------------------------------------------------
# PROGRAM'S MAIN LOGIC

startUp()
processRecords()
closeUp()

raw_input("\nRun complete. Press the Enter key to exit.")

感谢大家的帮助。我真的很感激。抱歉,如果我在此过程中让任何人感到沮丧。祝你有个好的一天。和平

最佳答案

我只是弄清楚函数 readRecord() 中代码中的一个错误,您只是在读取文件的第一行;您应该遍历所有行或使 readRecord() 成为生成器。

  def readRecord():
global name, course, grade, eof

studentRecord = gradeFile.readline() # <----- HERE

if studentRecord == "":
eof = True
else:
name = studentRecord[0:20]
course = studentRecord[20:50]
grade = studentRecord[50:51]
eof = False

但尽管如此,说实话,我不太喜欢你的代码,如果我是你,我会这样做:

1) 以任何方式从文件中获取数据 (csv, regex ...) ;我想我们已经在 here 中找到了答案

2) 将数据放入字典或其他任何东西中(这样您就可以随心所欲地操作它们)。

3) 使用itertools.groupby()按学生分组并计算您想要的总和。

4) 使用字符串 Template() ,因为格式可能会改变,并且不要像您那样对输出格式进行硬编码。

并且请在编写完成后逐一测试您的功能,因为如果您不这样做,将很难找出代码的哪一部分不工作。

编辑:

而且我不会问你为什么要这样做,因为如果你想再次将它们放入文件中,如果你想再次检索它们,你将遇到与以前相同的问题,如果你的目标只是做出漂亮的输出我会问你值得吗?

最后一个建议使用众所周知的格式,如 csv、xml ...

祝你好运:)

关于python - 让我的输出以某种方式显示时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4010714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com