打印声明不向我提供准确的数字

时间:2017-11-02 02:35:53

标签: python while-loop formatting sum average

好的,这是问题,代码有效,但数字没有加起来,所以我在某处遗漏了某些东西。我的大脑已经完全死了三天,所以我需要另一组眼睛。基本上这是一个评分计算器,它从.txt文件中获取输入并将输出发送到.txt文件。我正在放弃最低的测验分数。我还需要一些outfile.write(outstring)格式化的帮助,它现在到处都是,我需要它看起来与输出文件完全一样。我留下了一些诊断打印语句,但基本上这就是代码现在的样子:

infilename = input("Please enter file name: ")
outfilename = input("Please enter output file name: ")

infile = open(infilename,"r")
outfile = open(outfilename, "w")

quiz_sum = 0
low_quiz = 1000
prog_sum = 0

nameline = infile.readline()
outfile.write(nameline)
print(nameline)
while nameline != "":
    quiz_line = infile.readline()
    prog_line = infile.readline()
    exam_line = infile.readline()
    quiz_list=quiz_line.split()
    quiz_sum = 0
    prog_sum = 0
    low_quiz = 1000
    for quiz_score in quiz_list:
        quiz_float = float(quiz_score)
        quiz_sum += quiz_float
        if quiz_float < low_quiz:
            low_quiz = quiz_float
    if len(quiz_list) > 1:
        quiz_avg = ((quiz_sum-low_quiz)/(len(quiz_list)-1))*10
    else:
        quiz_avg = quiz_sum*10
    print("QUIZ AVERAGE", quiz_avg)
    prog_list = prog_line.split()
    for prog_score in prog_list:
        prog_float = float(prog_score)
        prog_sum += prog_float
    if len(prog_list) > 1:
        prog_avg = ((prog_sum) / len(prog_list))*10
    else:
        prog_avg = prog_sum*10
    print("PROGRAM AVERAGE", prog_avg)
    exam_list = exam_line.split()
    for exam_score in exam_list:
        exam_float = float(exam_score)
        #exam_sum += exam_float
    exam1 = float(exam_list[0])
    exam2 = float(exam_list[1])
    exam3 = float(exam_list[2])
    exam_avg = (exam1*.20)+(exam2*.20)+(exam3*.25)
    print("EXAMs" , exam1,exam2,exam3)
    average = ((quiz_avg*.15)+(prog_avg*.20)+(exam_avg))
    if 90 <= average <= 100:
        final_grade = "A"
    if 80 <= average < 90:
        final_grade = "B"
    if 70 <= average < 80:
        final_grade = "C"
    if 60 <= average < 70:
        final_grade = "D"
    if 0 <= average < 60:
        final_grade = "F"
    print("TOTAL AVERAGE",average)
    outstring =("%-20s %3.1f   %s" %(nameline,average,final_grade))
    print(outstring)
    outfile.write(outstring)
    nameline = infile.readline()
    outstring = ("%-20s***" % (nameline))

infile.close()
outfile.close()

这是输入的样子(第一行数字是测验,第二行是程序项目,第三行是考试[分别是考试1,2,3]):

Babbage, Charles
10.0   9.5    8.0  9.0
 9.5  10.0    9.0
85.0  92.0   81.0
Turing, Alan
1.0   8.0    6.0
10.0  10.0    9.0   9.5
90.0  92.0   88.5
Hopper, Grace
10.0  5.0    8.0
10.0  10.0    9.0   9.5
90.0  92.0   88.0
Van Rossum, Guido
 7.5   8.5
 7.5   6.0    6.0   9.0
68.0  81.0   70.0
Backus, John
 9.5   3   10.0   8.0   9.5  10.0
 7.5   8.5    6.0   9.0  10.0
99.0  93.0  44.0
Crawley, Bryan
 6.0
 7.5
70.0  60.0   55.5

最后这应该是输出的格式化:

Babbage, Charles      89.4   B
Turing, Alan          91.3   A
Hopper, Grace         92.7   A
Van Rossum, Guido     75.5   C
Backus, John          94.4   A
Crawley, Bryan        63.9   D

tl; dr - 我需要为这个程序的平均值得到正确的数字。我还需要有关程序在循环结束时如何写入文件的格式的帮助。谢谢你们。

编辑 -

更新帖子以显示我运行程序时会发生什么:     请输入文件名:scores.txt     请输入输出文件名:results.txt     巴贝奇,查尔斯

QUIZ AVERAGE 95.0
PROGRAM AVERAGE 95.0
EXAMs 85.0 92.0 81.0
TOTAL AVERAGE 88.9
Babbage, Charles
    88.9   B
QUIZ AVERAGE 70.0
PROGRAM AVERAGE 96.25
EXAMs 90.0 92.0 88.5
TOTAL AVERAGE 88.275
Turing, Alan
        88.3   B
QUIZ AVERAGE 90.0
PROGRAM AVERAGE 96.25
EXAMs 90.0 92.0 88.0
TOTAL AVERAGE 91.15
Hopper, Grace
       91.2   A
QUIZ AVERAGE 85.0
PROGRAM AVERAGE 71.25
EXAMs 68.0 81.0 70.0
TOTAL AVERAGE 74.3
Van Rossum, Guido
   74.3   C
QUIZ AVERAGE 94.0
PROGRAM AVERAGE 82.0
EXAMs 99.0 93.0 44.0
TOTAL AVERAGE 79.9
Backus, John
        79.9   C
QUIZ AVERAGE 60.0
PROGRAM AVERAGE 75.0
EXAMs 70.0 60.0 55.5
TOTAL AVERAGE 63.875
Crawley, Bryan
      63.9   D

这就是“results.txt”文件中的内容:

Babbage, Charles
Babbage, Charles
    88.9   BTuring, Alan
        88.3   BHopper, Grace
       91.2   AVan Rossum, Guido
   74.3   CBackus, John
        79.9   CCrawley, Bryan
      63.9   D

1 个答案:

答案 0 :(得分:0)

infilename = input("Please enter file name: ")
outfilename = input("Please enter output file name: ")

infile = open(infilename,"r")
outfile = open(outfilename, "w")

quiz_sum = 0
low_quiz = 1000
prog_sum = 0

nameline = infile.readline()
# outfile.write(nameline) # no need to have this
# print(nameline)
while nameline != "":
    quiz_line = infile.readline()
    prog_line = infile.readline()
    exam_line = infile.readline()
    quiz_list=quiz_line.split()
    quiz_sum = 0
    prog_sum = 0
    low_quiz = 1000
    for quiz_score in quiz_list:
        quiz_float = float(quiz_score)
        quiz_sum += quiz_float
        if quiz_float < low_quiz:
            low_quiz = quiz_float
    if len(quiz_list) > 1:
        quiz_avg = ((quiz_sum-low_quiz)/(len(quiz_list)-1))*10
    else:
        quiz_avg = quiz_sum*10
    print("QUIZ AVERAGE", quiz_avg)
    prog_list = prog_line.split()
    for prog_score in prog_list:
        prog_float = float(prog_score)
        prog_sum += prog_float
    if len(prog_list) > 1:
        prog_avg = ((prog_sum) / len(prog_list))*10
    else:
        prog_avg = prog_sum*10
    print("PROGRAM AVERAGE", prog_avg)
    exam_list = exam_line.split()
    for exam_score in exam_list:
        exam_float = float(exam_score)
        #exam_sum += exam_float
    exam1 = float(exam_list[0])
    exam2 = float(exam_list[1])
    exam3 = float(exam_list[2])
    exam_avg = (exam1*.20)+(exam2*.20)+(exam3*.25)
    print("EXAMs" , exam1,exam2,exam3)
    average = ((quiz_avg*.15)+(prog_avg*.20)+(exam_avg))
    if 90 <= average and average <= 100:
        final_grade = "A"
    if 80 <= average and average < 90:
        final_grade = "B"
    if 70 <= average and average < 80:
        final_grade = "C"
    if 60 <= average and average < 70:
        final_grade = "D"
    if 0 <= average and average < 60:
        final_grade = "F"
    print("TOTAL AVERAGE",average)
    outstring = ("{0:<20} {1:<8} {2:<25}\n".format(nameline.strip(), round(average,1), final_grade))
    print(outstring)
    outfile.write(outstring)
    nameline = infile.readline()
    outstring = ("%-20s***" % (nameline))

infile.close()
outfile.close()

我有固定的输出格式。现在它看起来像跟随。

结果输出文件:

Babbage, Charles     88.9     B                        
Turing, Alan         88.3     B                        
Hopper, Grace        91.2     A                        
Van Rossum, Guido    74.3     C                        
Backus, John         79.9     C                        
Crawley, Bryan       63.9     D                        

consolt输出:

('QUIZ AVERAGE', 95.0)
('PROGRAM AVERAGE', 95.0)
('EXAMs', 85.0, 92.0, 81.0)
('TOTAL AVERAGE', 88.9)
Babbage, Charles     88.9     B                        

('QUIZ AVERAGE', 70.0)
('PROGRAM AVERAGE', 96.25)
('EXAMs', 90.0, 92.0, 88.5)
('TOTAL AVERAGE', 88.275)
Turing, Alan         88.3     B                        

('QUIZ AVERAGE', 90.0)
('PROGRAM AVERAGE', 96.25)
('EXAMs', 90.0, 92.0, 88.0)
('TOTAL AVERAGE', 91.15)
Hopper, Grace        91.2     A                        

('QUIZ AVERAGE', 85.0)
('PROGRAM AVERAGE', 71.25)
('EXAMs', 68.0, 81.0, 70.0)
('TOTAL AVERAGE', 74.3)
Van Rossum, Guido    74.3     C                        

('QUIZ AVERAGE', 94.0)
('PROGRAM AVERAGE', 82.0)
('EXAMs', 99.0, 93.0, 44.0)
('TOTAL AVERAGE', 79.9)
Backus, John         79.9     C                        

('QUIZ AVERAGE', 60.0)
('PROGRAM AVERAGE', 75.0)
('EXAMs', 70.0, 60.0, 55.5)
('TOTAL AVERAGE', 63.875)
Crawley, Bryan       63.9     D                        

如果您可以提供如何计算的问题,我可以进行成绩计算(建议单独提问)。