我可以使用一些帮助来解决错误。这是我的代码,它接受多个输入和输出(输入量、错过考试的学生数量、成绩高于 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
如何固定我的平均值,使其与我的教授相同?
您将最后一个"-1"作为数据集的一部分。
另外,你为什么要这样做:
average2=total/(count+-numMiss)
与此相反:
average2=total/(count-numMiss)
第一个问题是你包含了-1
.你应该做
while True:
...
score=int(input("Enter a Score(-1 to quit): "))
if score == -1:
break
...
您只需要此行一次:
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