在python中的成绩报告哨兵循环测试

时间:2013-09-27 22:15:30

标签: python

我可以使用一些帮助来解决错误。这是我的代码,它包含多个输入和输出(输入量,错过测试的学生数量,获得高于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

如何修正我的平均值,使其与我的教授相同?

3 个答案:

答案 0 :(得分:1)

您将最后的“-1”包含在数据集中。

另外,你为什么这样做:

average2=total/(count+-numMiss)

与此相反:

average2=total/(count-numMiss)

答案 1 :(得分:1)

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

while True:
    ...

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

    if score == -1:
        break

    ...

答案 2 :(得分: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