首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >蟒蛇体感环试验分级报告

蟒蛇体感环试验分级报告
EN

Stack Overflow用户
提问于 2013-09-27 22:15:29
回答 3查看 504关注 0票数 0

我需要帮助解决一个错误。这是我的代码,它接受多个输入和输出(输入的数量,错过考试的学生的数量,分数高于80的学生,参加考试的学生的平均数,以及整个班级的平均水平,包括那些未通过考试的学生)。

代码语言:javascript
运行
复制
#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

这就是我的教授的输出

等级报告

代码语言:javascript
运行
复制
 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

这就是我得到的输出

等级报告

代码语言:javascript
运行
复制
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

我如何修正我的平均数,使它们与我的教授相同?

EN

回答 3

Stack Overflow用户

发布于 2013-09-27 22:20:06

你把最后一个"-1“作为数据集的一部分。

还有,你为什么要这样做:

代码语言:javascript
运行
复制
average2=total/(count+-numMiss)

与此相反的是:

代码语言:javascript
运行
复制
average2=total/(count-numMiss)
票数 1
EN

Stack Overflow用户

发布于 2013-09-27 22:23:11

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

代码语言:javascript
运行
复制
while True:
    ...

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

    if score == -1:
        break

    ...
票数 1
EN

Stack Overflow用户

发布于 2013-09-27 22:32:05

您只需要这一行一次:

代码语言:javascript
运行
复制
score = int(input("Enter a Score(-1 to quit):     "))

它应该在循环内。

代码应该如下所示:

代码语言:javascript
运行
复制
while True:
    score = int(input("Enter a Score(-1 to quit):     "))
    if score <= -1:
        break

此外,这一行还可以在循环之外移动:

代码语言:javascript
运行
复制
students = count
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19060931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档