0

我可以使用一些帮助来解决错误。这是我的代码,它接受多个输入和输出(输入的数量、未参加考试的学生数量、成绩高于 80 的学生、参加考试的学生的平均值以及整个班级,包括那些错过的人。

#number of students who miss, and get a highscore
num_miss = 0
num_high = 0
total = 0
count = 0

#The minimum and maximum values
min_score = 100.00
max_score = 0.0

score = int(input("Enter a Score(-1 to quit):     "))
#The loop to test score inputs, and the students who missed or got a high score
while score >-1:
    if score >= 80:
        num_high += 1
    elif score == 0:
        num_miss += 1

    if score < min_score:
        min_score = score
    if score > max_score:
        max_score = score

    score = int(input("Enter a Score(-1 to quit):     "))
    # an incriment to count the total for average, and the amount of students that took the exam
    total += score
    count += 1
    students = count
#formulas for creating the averages
average = total / count
average2 = total / (count + -num_miss)

#prints grade report with a dashed line under it    
print("Grade Report")
print("-" * 38)

if count >= 0:
    print("Total number of students in the class:      {}".format(students))
    print("Number of students who missed the exam:     {}".format(num_miss))
    print("Number of students who took the exam:       {}".format(count - num_miss))
    print("Number of students who scored high:         {}".format(num_high))
    print("Average of all students in the class:       {0:.2f}".format(average))
    print("Average of students taking the exam:        {0:.2f}".format(average2))

这就是问题所在:通过输入所有这些值:65、98、45、76、87、94、43、0、89、80、79、0、100、55、75、0、77、-1 到结束


这就是我的教授得到的输出


成绩报告

 1. Total number of students in the class: 17 
 2.Number of students who missed the exam: 3
 3.Number of students who took the exam: 14 
 4.Numberof students who scored high: 6 
 5.Average of all students in the class:62.5 
 6.Average of students taking the exam: 75.9

这就是我得到的输出


成绩报告

1.Total number of students in the class:      17
2.Number of students who missed the exam:     3
3.Number of students who took the exam:       14
4.Number of students who scored high:         6
5.Average of all students in the class:       58.64
6.Average of all students taking the exam:    71.21

我如何修正我的平均成绩,使他们与我的教授一样?

4

3 回答 3

1

第一个问题是您包含-1. 你应该做

while True:
    ...

    score=int(input("Enter a Score(-1 to quit):     "))

    if score == -1:
        break

    ...
于 2013-09-27T22:23:11.250 回答
1

您将最后一个“-1”作为数据集的一部分。

另外,你为什么这样做:

average2=total/(count+-numMiss)

与此相反:

average2=total/(count-numMiss)
于 2013-09-27T22:20:06.490 回答
0

您只需要此行一次:

score = int(input("Enter a Score(-1 to quit):     "))

它应该在循环内。

代码应如下所示:

while True:
    score = int(input("Enter a Score(-1 to quit):     "))
    if score <= -1:
        break

此外,这条线可以移出循环:

students = count
于 2013-09-27T22:32:05.613 回答